String constructor

Hi,

I got a code like this:

String s = ""; int c = 48;

s += String(c);

// I got a string "48" instead of "0", how to get "0" in this case?

Thanks,

V.C.

I got a string "48" instead of "0", how to get "0" in this case?

String s = "";
int c = 0;

s += String(c);

If 48 represents the ASCII value of a number, then

char c = 48;

By the way, there is no reason to create a String object from c,

s += c;

will work for c as an int or c as a character.

Thanks. the actual code is:

String s = ""; while (Serial.available()) { int c = Serial.read() s += String(c); }

httpPost(s)

// purpose is to see what has been read from the Serial device.

Thanks. the actual code is:

The Serial.read() function reads a byte from the serial buffer. It returns that byte in the low byte of an int, because every possible byte value is legitimate, and Serial.read() needs to be able to return an error. It does this using the high byte.

If you KNOW that Serial.read() will succeed, because Serial.available() says that there IS data to read, you can store the returned value in a byte-sized variable, like a char.

The String += operator is overloaded. It know how to append a char to the string, and how to append an int to the string. You want it to use the "append a char" method even though you are calling it with an int. That won't happen. You need to call the correct function, either by calling it with an explicit char or by casting the int to a char.

String s += c; when c is a char, or String s += (char)c; when c is an int.