I am trying to do an hexadecimal to integer conversion on a 32 bit machine. Here is the code I am testing with,
int main(int argc,char **argv)
{
char *hexstring = "0xffff1234";
long int n;
fprintf(stdout, "Conversion results of string: %s\n", hexstring);
n = strtol(hexstring, (char**)0, 0); /* same as base = 16 */
fprintf(stdout, "strtol = %ld\n", n);
n = sscanf(hexstring, "%x", &n);
fprintf(stdout, "sscanf = %ld\n", n);
n = atol(hexstring);
fprintf(stdout, "atol = %ld\n", n);
fgetc(stdin);
return 0;
}
This is what I get:
strtol = 2147483647 /* = 0x7fffffff -> overflow!! */
sscanf = 1 /* nevermind */
atol = 0 /* nevermind */
As you see, with strtol I get an overflow (I also checked with errno), although I would expect none to happen, since 0xffff1234 is a valid integer 32bit value. I would either expect 4294906420 or else -60876
What am I missing?