Return Value of atoi and strtoul

Discussion in 'C Programming' started by sachinahuja82@gmail.com, Apr 16, 2007.

  1. Guest

    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.
     
    , Apr 16, 2007
    #1
    1. Advertising

  2. 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.

    <snip>

    > 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Apr 16, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    wrote:
    >
    > 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



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Apr 16, 2007
    #3
  4. Thad Smith Guest

    Richard Heathfield wrote:
    > 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.

    --
    Thad
     
    Thad Smith, Apr 17, 2007
    #4
  5. Flash Gordon Guest

    Thad Smith wrote, On 17/04/07 04:06:
    > Richard Heathfield wrote:
    >> 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.
    --
    Flash Gordon
    Premature optimisation is the root of most evil.
    Premature deoptimisation is the root of a lot of the remaining evil.
     
    Flash Gordon, Apr 17, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. senthil kumar

    PPC and atoi

    senthil kumar, May 1, 2004, in forum: C++
    Replies:
    4
    Views:
    482
    John Harrison
    May 2, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    887
    Keith Thompson
    Mar 6, 2005
  3. Bill Cunningham

    atoi return

    Bill Cunningham, Oct 10, 2008, in forum: C Programming
    Replies:
    33
    Views:
    1,449
    Richard Bos
    Oct 17, 2008
  4. arnuld

    strtoul() behavior

    arnuld, Jan 5, 2011, in forum: C Programming
    Replies:
    39
    Views:
    3,239
    arnuld
    Jan 12, 2011
  5. Ivan Shmakov

    strtoul () vs. errno

    Ivan Shmakov, May 4, 2012, in forum: C Programming
    Replies:
    3
    Views:
    618
    Bill Cunningham
    May 4, 2012
Loading...

Share This Page