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.
I had problems with toupper and others also. My sugestion : Write your own function or wait untill it is'fixed by a new release.
Hi Viktor, thank you for your replay. This bugs are unbelievable...
If it is not a problem, can you tell me the complete list of the functions with bugs that you have found?
In this way I could know what to modify...
Thank you
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"
sbuffer [] = " 10 ";
the result is lvar = 0 and p = " "
sbuffer [] = "11w ";
the result is lvar = 11 and p = "w "
sbuffer [] = "12w";
the result is lvar = 2 and p = "w"
sbuffer [] = " 20";
the result is lvar = 0 and p = "20"
sbuffer [] = "51 ";
the result is lvar = 51 and p = " "
sbuffer [] = "w51 ";
the result is lvar = 0 and p = "w51 "
sbuffer [] = "w 51";
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.
sorry, I cannot replicate any of these problems. What compiler are you using? Do you have an issue with the memory on these devices?
Hi Reinhard, thank you for your attention and patience.
The problem is very simple. I've a problem with strtol function (included in stdlib). I'm using uVision Ide 3 (with RealView 3.0.0.951 compiler) and NXP LPC2148.
The conversion of a numerical value inside a string, works only under some particular conditions and not in every case (if the numerical value is valid), as I have always seen on other C compilers.
For example, the simple conversion of:
long lvar; char sbuffer [] = "12"; char *p; lvar = strtol (sbuffer, &p, 10);
It doesn't produce the expected value = 12, but 0.
Instead if you put a "+" sign before the number to convert strtol works. For example sbuffer [] = "+12", the result is = 12.
Also if you put a space after the number to convert it works. For example sbuffer [] = "12 ", the result is = 12.
But "12" or " 12" or "12w" or " 12 " or "1,2" don't work. (In the case of "12w" the result is "2"...)
Thank you. M.