Click to See Complete Forum and Search --> : Convertion of hex string to integer...


Serge Darii
April 1st, 1999, 07:31 AM
Hi all,


Does anyone know how can I convert hexadecimal string to int or long type

using MFC or other libs.

For example:

char *hexstr = "101E4";

int i = ?HexToIntFunc?(hexstr);


Thanks in advance


Serge

Bore
April 1st, 1999, 07:45 AM
It's not an mfc function, but I don't know of any mfc functions that do it.

June 18th, 1999, 05:08 PM
You can convert hex number to int by using sscanf and %X - reading hex like this.

char *inp = "A1D2" ;
int i;
sscanf( inp, "%X", &i ) ;

It's as simple as that.

Best Wishes,
Sriram.

June 18th, 1999, 05:11 PM
strtol is for long. It can't accept hex strings.

chiuyan
June 18th, 1999, 05:49 PM
why can't strtol except a hex number? a hex number is still a long, Hex is not a data type, it is a certain base of a number.
strtol looks like

long strtol( const char *nptr, char **endptr, int base );




just set the base to 16.

--michael

Gomez Addams
June 18th, 1999, 06:43 PM
One really handy feature is that if you pass a base of 0 it will interpret the string
based on the first characters it finds. If the string starts with '0x' it will use a hex
interpretation. If it starts with a zero and not an X then it uses octal.
This convention allows you to mix types and strtol will sort things out for you.

G Squared
June 18th, 1999, 08:14 PM
Of course, Its more fun to write your own function to convert the string into hex.
~G²