This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atoi, strol functions problems

Hello,

I work with LPC2148 and RealView Compiler.

I have noticed that if I convert a string with the atoi function, the result never is correct.
If the value of the number in the string is inferior to 20 the result is 0. If the value of the number is great of 20 the result is equal to the number subtracted to 20.

Example 1:

char sbuffer[] = "12";
int buffer;
buffer = atoi(sbuffer);
//now buffer = 0 (I waited for 12).

Example 2:

char sbuffer[] = "29";
int buffer;
buffer = atoi(sbuffer);
//now buffer = 9 (I waited for 29).

Probably the problem is with strtol (function used by atoi), perhaps with the number base? But the number base of strtol is not automatic with atoi?

Is there some suggestion? Thank you.

M.

Parents
  • I have tested others strange behavior in the strtol function, therefore atoi or atol. I have never found these problems with other C compilers.

    This is the code:

    
      long lvar;
      char sbuffer [] = "";
      char *p;
      lvar = strtol (sbuffer, &p, 10);
    
    

    and these are the results;

    with

    char sbuffer [] = "+1,2";
    

    the result is lvar = 1 and p = ",2"

    with

    sbuffer [] = " 10 ";
    

    the result is lvar = 0 and p = " "

    with

    sbuffer [] = "11w ";
    

    the result is lvar = 11 and p = "w "

    with

    sbuffer [] = "12w";
    

    the result is lvar = 2 and p = "w"

    with

    sbuffer [] = " 20";
    

    the result is lvar = 0 and p = "20"

    with

    sbuffer [] = "51 ";
    

    the result is lvar = 51 and p = " "

    with

    sbuffer [] = "w51 ";
    

    the result is lvar = 0 and p = "w51 "

    with

    sbuffer [] = "w 51";
    

    the result is lvar = 51 and p = " "

    In conclusion:

    With the sign before the number to convert strtol works. With a space before the number to convert it doesn't work. With a space after the number number to convert it works.

    Has nobody realized this problem?

    Thank you M.

Reply
  • I have tested others strange behavior in the strtol function, therefore atoi or atol. I have never found these problems with other C compilers.

    This is the code:

    
      long lvar;
      char sbuffer [] = "";
      char *p;
      lvar = strtol (sbuffer, &p, 10);
    
    

    and these are the results;

    with

    char sbuffer [] = "+1,2";
    

    the result is lvar = 1 and p = ",2"

    with

    sbuffer [] = " 10 ";
    

    the result is lvar = 0 and p = " "

    with

    sbuffer [] = "11w ";
    

    the result is lvar = 11 and p = "w "

    with

    sbuffer [] = "12w";
    

    the result is lvar = 2 and p = "w"

    with

    sbuffer [] = " 20";
    

    the result is lvar = 0 and p = "20"

    with

    sbuffer [] = "51 ";
    

    the result is lvar = 51 and p = " "

    with

    sbuffer [] = "w51 ";
    

    the result is lvar = 0 and p = "w51 "

    with

    sbuffer [] = "w 51";
    

    the result is lvar = 51 and p = " "

    In conclusion:

    With the sign before the number to convert strtol works. With a space before the number to convert it doesn't work. With a space after the number number to convert it works.

    Has nobody realized this problem?

    Thank you M.

Children