Go Down

Topic: Any idea to convert ascii from serial to integer ? (Read 3464 times) previous topic - next topic


i want to convert ascii that recieve from serial by X35 and Y50

to keep in variable X,Y in arduino.

and other case if it in float like X35.5 and Y45.5

or minus case like X -35.5 and Y -45.5

i'am newbie please advice.


Read each byte as it comes in and build it up.

Identify the X or Y so you know what you are reading.
Convert the ascii to a digit :-
if(inValue > &0x2f & inValue< 0x3a) digit = inValue & 0x0f;
and accumulate the number, that is:-
number = (number * 10) + digit;
when you hit the point this becomes:-
number = number + digit/place; place++;
on a space or any non number you have completed the input of the number.

Or you could search to see when you can get code that does this for you.


Integers are simple - just buffer the characters until you get a non-numeric character, terminate the buffer with a '\0' to make a string and then use "atoi" (ASCII-to-integer).
Not sure if "atof" (ASCII-to-float) is implemented - it probably is.



Ok thank, i try atoi last night and it work very well,

tha hard thing is to fill '\0' in the last bucket

Go Up