Re: How do i convert unsigned char to Hex Value
From: Frank Hickman [MVP] (fhickman_NOSP_at_M_noblesoft.com)
Date: 02/05/05
- Next message: Suresh Chandra: "Re: FindFirstFile and Network Shares"
- Previous message: Alexander Grigoriev: "Re: realloc in function"
- In reply to: PK: "Re: How do i convert unsigned char to Hex Value"
- Next in thread: PK: "Re: How do i convert unsigned char to Hex Value"
- Reply: PK: "Re: How do i convert unsigned char to Hex Value"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 5 Feb 2005 01:11:07 -0500
"PK" <PK@discussions.microsoft.com> wrote in message
news:E43AB081-5592-428C-A10A-9466FF19679B@microsoft.com...
> Thanks Frank. I want to use the convertion (hex to char*) in a different
> application. Using MSMQ i will send this hex value to MSMQ private queue.
> Otherside the receiving application should convert the hex to char*.
> That's
> why i asked the convertion method.
>
Oh, you mean you need to convert it back to the original format? I modified
my previous code to two functions. One to convert to a string of hex
characters and the other returns it to the previous contents.
#define ERROR_SIZE ERROR_SUCCESS+1
char* bytestr2hexstr( char* lpBuffer, int cbBuffer, const unsigned char*
lpBytes, int cbInput )
{
if ( (cbInput*2+1) > cbBuffer )
{
SetLastError( ERROR_SIZE );
return NULL;
}
int n= 0;
while( n < cbInput )
{
sprintf( lpBuffer+2*n, "%02x", *(lpBytes+n) );
++n;
}
*(lpBuffer+2*n)= '\0';
SetLastError( ERROR_SUCCESS );
return lpBuffer;
}
unsigned char* hexstr2bytestr( unsigned char* lpBytes, int cbBuffer, const
char* lpBuffer, int cbInput )
{
if ( cbBuffer < (cbInput/2) )
{
SetLastError( ERROR_SIZE );
return NULL;
}
int n= 0;
while( n < cbInput )
{
sscanf( lpBuffer+2*n, "%02x", reinterpret_cast< char* >(
lpBytes+n ) );
++n;
}
SetLastError( ERROR_SUCCESS );
return lpBytes;
}
and tested it with the following code in a console application...
// Note: I used these values because they can be represented
// in the console output window as the letters A through Z.
unsigned char valBuffer[]= {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
0x4d,
0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a
};
char szBuffer[53];
if ( !bytestr2hexstr( szBuffer, 53, valBuffer, 26 ) )
{
cout << _T("bytestr2hexstr(") << GetLastError() << _T("): function
failed...") << endl;
return 2;
}
else
{
cout << szBuffer << endl;
unsigned char ucBuffer[26];
if ( !hexstr2bytestr( ucBuffer, 26, szBuffer, strlen( szBuffer ) ) )
{
cout << _T("hexstr2bytestr(") << GetLastError() << _T("): function
failed...") << endl;
return 3;
}
for( int i= 0; i < 26; i++ )
cout << ucBuffer[i];
cout << endl;
}
--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.
> "Frank Hickman [MVP]" wrote:
>
>> "PK" <PK@discussions.microsoft.com> wrote in message
>> news:C44617C2-6E03-434D-B6B5-450709BB1D59@microsoft.com...
>> > Thanks Frank. It worked fine. But i had to use memset to initialize the
>> > pointer. Otherwise it was giving me access violation.
>> >
>> > char *locBuffer;
>> > locBuffer = (char *)malloc(eBufferSize*2+1);
>> > memset(locBuffer, 0, eBufferSize*2+1);
>> > for(int i= 0; i < eBufferSize; i++)
>> > {
>> > static char hex[3];
>> > sprintf(hex, "%x", valBuffer[i]);
>> > strcat(locBuffer, hex);
>> > }
>> >
>>
>>
>> Actually, if you using the code without the function "hex" then you can
>> just
>> create a character buffer on the stack. It does not need to be static.
>> So
>> your code could be changed like so...
>>
>> char *locBuffer;
>> locBuffer = (char *)malloc(eBufferSize*2+1);
>> memset(locBuffer, 0, eBufferSize*2+1);
>> char hex[3];
>> for(int i= 0; i < eBufferSize; i++)
>> {
>> sprintf(hex, "%x", valBuffer[i]);
>> strcat(locBuffer, hex);
>> }
>>
>> > Can you tell me how i can revert this from hex to char*. Don't laugh.
>>
>> I don't understand your question. locBuffer is a char* already. It
>> points
>> to a character representation of the numbers converted to hexadecimal so
>> something like this would work to display it out on say a console window.
>>
>> printf( "%s\n", locBuffer );
>>
>> --
>> ============
>> Frank Hickman
>> Microsoft MVP
>> NobleSoft, Inc.
>> ============
>> Replace the _nosp@m_ with @ to reply.
>>
>>
>>
>>
- Next message: Suresh Chandra: "Re: FindFirstFile and Network Shares"
- Previous message: Alexander Grigoriev: "Re: realloc in function"
- In reply to: PK: "Re: How do i convert unsigned char to Hex Value"
- Next in thread: PK: "Re: How do i convert unsigned char to Hex Value"
- Reply: PK: "Re: How do i convert unsigned char to Hex Value"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|