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