|
-
April 1st, 1999, 08:31 AM
#1
Convertion of hex string to integer...
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
-
April 1st, 1999, 08:45 AM
#2
I use strtol()
It's not an mfc function, but I don't know of any mfc functions that do it.
-
June 18th, 1999, 05:08 PM
#3
Re: Convertion of hex string to integer...
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
#4
strtol won't work.
strtol is for long. It can't accept hex strings.
-
June 18th, 1999, 05:49 PM
#5
Re: strtol won't work.
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
-
June 18th, 1999, 06:43 PM
#6
Re: strtol won't work.
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.
-
June 18th, 1999, 08:14 PM
#7
Re: Convertion of hex string to integer...
Of course, Its more fun to write your own function to convert the string into hex.
~G²
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|