After searching hours in the net, not founding a good solution I ask in this great forum here
I have
int a;
char a_a[5];
int b;
char a_b[5];
char c[10];
void foo()
{
a = 5;
b = 3;
itoa(a,a_a,10);
itoa(b,a_b.10;
c = a+ “:”+b; // does not work
Serial.println(c);// should show “5:3” just for debug
// I need the char C[10] otherwise i.e for output to MQTT and LCD Display
}
All I found goes with strings, isn’t there a simpler way like in PHP i.e.
In fact you can optimize the two separate buffers away entirley by directly snprintf()-ing into the final buffer with %d to convert the ints to their string representations.
int a;
int b;
char c[10];
void foo()
{
a = 5;
b = 3;
snprintf(c, sizeof(c), "%d:%d", a, b);
Serial.println(c);// should show “5:3” just for debug
// I need the char C[10] otherwise i.e for output to MQTT and LCD Display
}
The + operator for string concatination is available in the Arduino String class though, so basing everything of String would look like
int a;
String a_a;
int b;
String a_b;
String c;
void foo()
{
a = 5;
b = 3;
a_a = String(a);
a_b = String(b);
c = a_a + ":" + a_b; // works
Serial.println(c);// should show “5:3” just for debug
//get pointer to raw C string / char*
const char* c_rawstr = c.c_str();
}
String is however a heap-allocated memory structure and not very memory efficient (see here), so you should prefer to work with C strings and the functions from string.h in memory-constrained envrionments (and for stability).The snprintf() version above is more optimal.