Re: How do i convert unsigned char to Hex Value

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Frank Hickman [MVP] (fhickman_NOSP_at_M_noblesoft.com)
Date: 02/05/05


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.
>>
>>
>>
>> 


Relevant Pages

  • Re: How do i convert unsigned char to Hex Value
    ... I want to use the convertion in a different ... Using MSMQ i will send this hex value to MSMQ private queue. ... Otherside the receiving application should convert the hex to char*. ... >> Thanks Frank. ...
    (microsoft.public.vc.language)
  • injection_projection code
    ... because of hex encoding. ... a row in the template. ... Sanitize string to protect against email header injection. ... void injection_protection(char *s) ...
    (freebsd-isp)
  • Help with sscanf()
    ... read a hex value for a MAC address given in the form of a string. ... Ultimately, I have to take the NICs MAC address (a 6 byte value, given ... of type char. ... seen that most folks use sscanf() to accomplish this. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: String to byte[] -- I cant get there from here?
    ... I need to convert these to a byte array of hex values. ... Each char contains two bytes. ... high nibble of each byte. ... Repeat for each char in the String. ...
    (comp.lang.java.programmer)
  • Re: How do i convert unsigned char to Hex Value
    ... Can you tell me how i can revert this from hex to char*. ... "Frank Hickman " wrote: ... >> Larry, I want to convert my pointer value to a hexadecimal representation, ...
    (microsoft.public.vc.language)