Reading a bit through the forums i got the message that i should try to avoid String() wherever i can. But i wonder how i could, for instance when i read a value from a sensor that is being returned as a int and want to send that to a webservice (in my case ubidots) that only takes String-Values.
I could convert the int to a char but the only way i found is through conversion to a String.
Is there any other way to circumvent String?
char example[80];
int volts=27;
int temperature=37;
sprintf( example, "volts=%d&temp=%d", volts, temperature );
client.print(example);
itoa is another possibility - if you're not using sprintf already, it looks like itoa would use less progmem.
wildbill makes a good point. Compiling the code using sprintf() on an Uno generated 3446 bytes of code. Using itoa():
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char example[50] = "volts=";
char temp[10];
int volts=27;
int temperature=37;
itoa(volts, temp, 10);
strcat(example, (const char *)temp);
itoa(temperature, temp, 10);
strcat(example, "&");
strcat(example, (const char *)temp);
Serial.println(example);
}
generated 2222 bytes of code. In most uses, sprintf() is an H-bomb-to-kill-an-ant approach and I usually try to avoid it if I can.
It is interesting to look over ways to convert an int to a C string in RAM but consider that getting that int to the webservice, isn't it done through print()?
Thank you very much altogether, used both sprintf and itoa, both work great for me!