I’m trying to create a string that’s made of multiple string parts and int variables.
Something like “Alarms: 0; Sales: 0”.
However, I can’t find a normal way of doing it and getting a good result. In the place of the integer variable, there’s a weird char that appear before the number.
I’ve tried using the + operator to combine multiple strings as well, but I get an error that I can’t use a the binary operator + on strings (or char[]…)
All of the formatted text could be done in one line if you use a real c string:
int number1 = 1;
int number2 = 2;
char line[32]; /* At least big enough for 31 chars + null. */
sprintf(line, "Number 1 - %d; Number 2 - %d\n", number1, number2);
Serial.print(line);
If your formatted text could be larger, be sure to increase the memory you’ve allocated for line to more than 32 bytes.
Thank you @BDub and @mattande.
I decided to choose @mattande’s approach, as it’s smaller and simpler (except for the predefined length of the line), and it’s working smoothly!
I’m still interested in what I did wrong in my original code, if someone has an idea.
Totally forgot about multiple variables in sprintf() while I was completely focused on getting int’s into String objects… there should really be an easier way… C++ has some, but none of them work in the Sparkulator