Arduino ASCII Decoding

Hey Guys,

this question could be a bit too easy, but I didn't find a solution in the internet.

I'm looking for a simple ASCII decoder. If I have a a serial.read() function in my Arduino code, the value i receive is in ascii numbers. If i send a "a" to the Arduino, I'm receiving a "97". Or: If i send "arduino", i get the answer "97 114 100 117 105 110 111 "

Does anybody know a function to change these ascii-numbers back to letters and numbers.

Thanks and Greetings, Hendrik

Serial.read() returns an integer character code so it can return -1 if no character is available.

If you pass that integer to Serial.print() it will assume you want the decimal value of that integer. You can use Serial.write() which will send the character code directly or you can 'cast' the integer as a char: Serial.print((char)integerCharacterCode). When Serial.print() gets a 'char' instead of an 'int' it will send the character.

If you want to do anything with the data received other than show it on the monitor, such as compare it with something, then you will need to read it into a variable with Serial.read(). The type of variable that you read the data into works like the cast suggested by John and automatically converts the data to that type.

Ok, Thanks

that works pretty good, but now i get the separated chars as response. I mean, if I send a string "arduino" to the controller, I receive a row of chars "a" "r" "d" "u" "i" "n" "o". Can I buffer the input to a array or something and convert the bunch of chars after i receive a end-sign back to a string.

String inString = "";
const int end_sign = '\n');

void loop() {
    int inChar = Serial.read();
    if (inChar == -1)  // No character
        return;

    if (inChar == end_sign) {
        Serial.print("\"");
        Serial.print(inString);
        Serial.println("\"");
        inString = "";
    } else {
        inString += (char)inChar;
    }
}

Two things wrong with that code ;)

1: There's an extra ) you might want to hunt down and eradicate. 2: It uses the String object - something we don't like to encourage...