loading

Arduino: How do I convert a decimal to ASCII?

I'm following this Instructable which is about communicating between 2 Arduinos with RF modules. The received value is stored in buff[0]  and if this value is "1", a light switches on. However, I was going to experiment with sending values other that 1 or 0 from the other Arduino and first added Serial.println(but[0]) but in the serial monitor, it read 49, which if you treat as a decimal value and convert ASCII, you get "1". Equally, when "0" was sent from the sending Arduino, the monitor read 48, which is decimal of the ASCII value "1".
I'm a bit confused as the if-statement below works fine.

Basically, I want "1" and "0" to be printed to the serial monitor not 48 and 49. How do I do this?

Thanks,
theRedBryophyte

Serial.println(buf[0]);
if(buf[0]=='1'){
  //some code here...
}

The contents of the buffer are ASCII values, since you loaded buf[0]='1'

To print as you want say Serial.println( buf[0]-'0' )

theredbryophyte (author)  steveastrouk1 year ago

Thank you - that sorted the problem out.

nvidia4010 months ago

hello....i have the same problem as you.check out the following link. definitely you will understand what to do:

http://arduiny.com/convert-ascii-character-string-...