Return Value of atoi and strtoul

S

sachinahuja82

Hi
I am using atoi and strtoul in my code.
I have read from MSDN that both functions return 0 if they are
unsuccessful.
i.e strtoul returns 0 if no conversion can be performed.
Now the string that I want to convert is "0x0"
The code that I am using is

char *Temp = NULL;
char sVal [10] = "0x0";
unsigned long ulReturnValue = strtoul(sVal , &Temp , 16);
Now the value that I get in ulReturnValue is equal to 0.
So how can i do error handling here?
If i use the following code for my error handling

if(ulReturnValue == 0)
printf("Error in Conversion");

Now as i am getting value of ulReturnValue as 0 .So the above code
also gets executed.
So could you please let me know why this function returns 0 when
conversion cannot be performed.
And what should I do if I have "0x0" string to convert.


The same problem arises with atoi.atoi returns 0 if input cannot be
converted.
If we have string "0" to be converted to integer.
Then we cannot do error handling in this.

Please suggest me with some solution for the above problem.
Thanks in advance.
 
R

Richard Heathfield

(e-mail address removed) said:
I am using atoi and strtoul in my code.
I have read from MSDN that both functions return 0 if they are
unsuccessful.
i.e strtoul returns 0 if no conversion can be performed.
Now the string that I want to convert is "0x0"
The code that I am using is

char *Temp = NULL;
char sVal [10] = "0x0";
unsigned long ulReturnValue = strtoul(sVal , &Temp , 16);
Now the value that I get in ulReturnValue is equal to 0.
So how can i do error handling here?

On return, Temp will point to the first unconverted character. If Temp >
sVal, then you know some conversion was done, so the result can be
assumed to be correct even though the return value is 0.

The same problem arises with atoi.

Don't use it. Unlike strtoul (and strtol and strtod), it is not robust,
for precisely the reason you have discovered.
 
C

CBFalconer

I am using atoi and strtoul in my code. I have read from MSDN
that both functions return 0 if they are unsuccessful. i.e
strtoul returns 0 if no conversion can be performed. Now the
string that I want to convert is "0x0"
From N869:

Returns

[#8] The strtol, strtoll, strtoul, and strtoull functions
return the converted value, if any. If no conversion could
be performed, zero is returned. If the correct value is
outside the range of representable values, LONG_MIN,
LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is
returned (according to the return type and sign of the
value, if any), and the value of the macro ERANGE is stored
in errno.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
T

Thad Smith

Richard said:
(e-mail address removed) said:
char *Temp = NULL;
char sVal [10] = "0x0";
unsigned long ulReturnValue = strtoul(sVal , &Temp , 16);
Now the value that I get in ulReturnValue is equal to 0.
So how can i do error handling here?

On return, Temp will point to the first unconverted character. If Temp >
sVal, then you know some conversion was done, so the result can be
assumed to be correct even though the return value is 0.

Better yet, confirm that the entire string has been bypassed:
strlen(Temp) == 0. If trailing whitespace is an issue, trim before the
conversion.
 
F

Flash Gordon

Thad Smith wrote, On 17/04/07 04:06:
Richard said:
(e-mail address removed) said:
char *Temp = NULL;
char sVal [10] = "0x0";
unsigned long ulReturnValue = strtoul(sVal , &Temp , 16);
Now the value that I get in ulReturnValue is equal to 0.
So how can i do error handling here?

On return, Temp will point to the first unconverted character. If Temp
sVal, then you know some conversion was done, so the result can be
assumed to be correct even though the return value is 0.

Better yet, confirm that the entire string has been bypassed:
strlen(Temp) == 0. If trailing whitespace is an issue, trim before the
conversion.

If you don't care about the length then calling is a waste. Depending
on taste, *Temp!=0, !*Temp os some variation there of.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

atoi return 33
a question abou "atoi" 6
Getting value of instances of variable. 1
problematic atoi conversion 2
question about atoi 3
Help with atoi function for a numero program. 4
atoi query 13
atoi question 7

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top