Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CString to hex ?

185 views
Skip to first unread message

David Excoffier

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
Hi all.
I have a CString containing datas in hexa. (E.g.: "FF", "C4", "D8",
"85", ...)

What is the simplest way to convert this string to an hexadecimal value
???

TIA.


Francis Clark

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
I think you can use strtol and use base 16, something like that anyway.

David Excoffier <david.e...@c-s.fr> wrote in message
news:393F4CE6...@c-s.fr...

Scooter

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
Something like this (not tested) .......

char *string, *stopstring;
string = "0xFF";
l = strtol( string, &stopstring, 16 );

Sc

Francis Clark <franci...@tangozebra.com> wrote in message
news:393f6...@nnrp1.news.uk.psi.net...

Brian Genisio

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
You could always use this function : It uses the integer value of the
character to determine the actual integer value, (Upper or Lowercase) in
the TranslateChar function. It adds it to the value, and shifts it to the
left 4 bits. This particular example is for 8 character hex (32 bit
integers) but can be chaged to 4 char (16 bit) or 2 char (8 bit) by simply
changing the loop condition intCharCount < 8 to the correct value.

Hope it helps,
Brian Genisio

lngTempWord = 0;
for(intCharCount = 0; intCharCount < 8; intCharCount++)
{
lngTempWord = lngTempWord << 4;
lngTempWord += TranslateChar( charInString[ intStringPos ] );
intStringPos++;
}

int TranslateChar( char charIn )
{
int intCharIn = (int)charIn;

// If the character value in ASCII is between 0 and 9, subtract the 0
value
if( (intCharIn >= (int)'0') && (intCharIn <= (int)'9') )
return ( intCharIn - (int)'0');

// If the character value in ASCII is between A and F, subtract the A
value and add 10
if( (intCharIn >= (int)'A') && (intCharIn <= (int)'F') )
return ( intCharIn - (int)'A' + 10 );

// If the character value in ASCII is between a and f, subtract the a
value and add 10
if( (intCharIn >= (int)'a') && (intCharIn <= (int)'f') )
return ( intCharIn - (int)'a' + 10 );

return 0;

David A. Lethe

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
On Thu, 08 Jun 2000 09:36:06 +0200, David Excoffier
<david.e...@c-s.fr> wrote:

>Hi all.
>I have a CString containing datas in hexa. (E.g.: "FF", "C4", "D8",
>"85", ...)
>
>What is the simplest way to convert this string to an hexadecimal value
>???
>
>TIA.
>

I've always just scanf'd.

sscanf(data,"%X",&longvalue);

david


0 new messages