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
Printable View
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
It's not an mfc function, but I don't know of any mfc functions that do it.
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.
strtol is for long. It can't accept hex strings.
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
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.
Of course, Its more fun to write your own function to convert the string into hex.
~G²