Weird message on serial monitor


Why does variable a give "198" instead of "0204" ?

s.ino (558 Bytes)

Post code, not pictures of code.

Done

Close, but no cigar.

What's the big deal that message [0] doesn't get invited to the party?

Because 48 + 50 + 48 + 52 is 198

What is the function I have to use to get "0204"

Do you mean you want "0204" or the octal value 0204?

"0204"

Using+ not substring

Do you want a String (or a string) or the octal value 0204?

String

How can I do it

The String operators are given here.

To append a char to a String, you want to use +=

The + is for the concatenation of Strings. message[n] is a char not a String.

//a=message[1]+message[2]+message[3]+message[4];
for (byte i= 1; i<=4; i++)
    { a += message[i];}

If you are going to use String objects in your code, then you should learn how to use them safely so that you don't run into memory issues.

See
How to Use Arduino Strings
and
Taming Arduino Strings

@drmpf will be along shortly to proselytise.

If you are intent on using String, then why not use the substring function instead of appending a single character at a time?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.