0

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.

5
  • int x = String( chars ).toInt() works if nothing else...
    – dandavis
    Commented Apr 21, 2017 at 9:30
  • You'd better post your char buffer content here. There is a chance it might contain something can't be converted into integer.
    – Sener
    Commented Apr 21, 2017 at 10:06
  • i am only sending integer data such as 242,etc
    – SChati
    Commented Apr 21, 2017 at 10:57
  • 1
    "not working" is not an useful description of a problem.
    – gre_gor
    Commented Apr 21, 2017 at 11:32
  • atoi should have worked. If it didn't then you have some other mistake. If you'll post the code that you were using then that would make it possible for someone to try to spot the problem and help you.
    – Delta_G
    Commented May 22, 2017 at 3:36

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.