-
Hex String to a Long
I'm getting a hexadecimal input from the command line and I can't seem to get it to convert. I know, I know, use strtol(). The problem is I keep getting zero as the result. I hard-code hex strings ("0x13","0XF") and they work fine. I print out the string on the line before the conversion and it looks fine too.
-
strtol can absolutely do this, so perhaps if you post your code, we can figure out what is going wrong.
--
Mats
-
Code:
result = GetFlag(nargs, args, 'S'); /* gets argument proceeding "-S" */
fprintf(stdout, "seed=%u \n",seed); /* prints default seed value */
fprintf(stdout, "result=%s \n",result); /* prints result string */
if (result != NULL)
seed = strtoul(result, NULL, 16); /* convert hex string to long */
fprintf(stdout, "seed=%u \n",seed); /* comes back zero */
result is a char*, and GetFlag returns a char*.
-
And what is the exact content of result (as per the printf)?
--
Mats
-
I'm the dumbest man alive... on my console it's hard to tell the difference between a '0' and an 'O'. I've been preceeding the numbers with the letter O this whole time. I guess auto-complete isn't always a good thing.
-
Yup, that would of cours make a difference - and give the result you describe. Congrats to finding it - and I think you'll find that there are at least a few others who are equally dumb [and the dumbest may actually go to a lady I worked with who wondered why the copies didn't show up - turned out that if you copy what is on the back of a single-sided page, you get pretty much "nothing" :)]
--
Mats