I am receiving a message from my mobile to my microcontroller via GSM. The message is in a char buffer, but I want to convert it to an integer as I want to perform some mathematical operations on it. I tried many functions such as atoi
, toint
but it's not working. Please let me know if there is any way in which I could do this conversion.
1 Answer
atoi()
is the simplest way:
int val = atoi(buf);
If that doesn't work then your buffer doesn't start with the number you are after. If that is the case you will have to work out where in the buffer the number is and convert just that portion. How you do that depends on what the format of the data is in your buffer.
int x = String( chars ).toInt()
works if nothing else...char buffer
content here. There is a chance it might contain something can't be converted into integer.