Joining several variables into a string

Lets say I have 4 intiger variables

int a =1;
int b =2
int c =0;

int d =0;

I want to find out the simplest way to join them into the string that I end up with
"12:00"
I managed to do it but I don't think I did it in the most optimal way.
I will not disclose how I did it , so you can keep an open mind

It depends. Are any of those variables going to be greater than 9 or less than 0? If not it can be really simple:

char foo[6];
.
.
.
foo[0] = a + '0';
foo[1] = b + '0';
foo[2] = ':';
foo[3] = c + '0';
foo[4] = d + '0';
foo[5] = '\0';

what does
+'0'
do?
My way was to use charAt

String s = (String) a + b + ":" + c + d;

to avoid a XY_problem ... please explain what you really want to achieve.

I managed to do it but I don't think I did it in the most optimal way.

In 99% it's not necessary anyway.

Lets say you have 4 integer variables

Why do you want to join them together in a string (a null terminated array of chars, aka a C string), or do you mean a String (an object created by the String library) ? They are not the same thing

Hello,

int a,b,c,d;

void loop() {
  char text[10];
  sprintf(text,"%02d:%02d",10*a+b,10*c+d);
}

The advantage of the code above is that you do not use C++ but C.
C is faster on a microcontroller and smaller in footprint in may cases.
You can also leave out the 02 format specifier if you want 1:00 instead of 01:00.
Best Regards,
Johi

The advantage of the code above is that you do not use C++ but C.
C is faster on a microcontroller and smaller in footprint in may cases.

Can you force the compiler to use C rather than C++ ?

Why bother with multiplication ?

  sprintf(text, "%d%d:%d%d", a, b, c, d);

As this question doesn't make so much sense for me anyway, yeah - let's use sprintf on the AVR.

#define VERSION 4
// V  PRGOGMEM  globals
// 0  2380      196
// 1  3932      204
// 2  3632      202
// 3  3614      202
// 4  2112      192

int a, b, c, d;

void setup() {
  Serial.begin(115200);
  a = random(0, 3);
  b = random (0, 10);
  c = random (0, 6);
  b = random (0, 10);

#if VERSION == 0
  Serial.print(a);
  Serial.print(b);
  Serial.print(":");
  Serial.print(c);
  Serial.print(d);

#elif VERSION == 1
  String s = (String) a + b + ":" + c + d;
  Serial.print(s);

#elif VERSION == 2
  char text[10];
  sprintf(text, "%02d:%02d", 10 * a + b, 10 * c + d);
  Serial.print(text);

#elif VERSION == 3
  char text[10];
  sprintf(text, "%d%d:%d%d", a, b, c, d);
  Serial.print(text);

#elif VERSION == 4
  char text[6];
  text[0] = a + 0x30;
  text[1] = b + 0x30;
  text[2] = ':';
  text[3] = c + 0x30;
  text[4] = d + 0x30;
  text[5] = '\0';
  Serial.print(text);
#endif


}

void loop() {

}

"if this is the solution - I want my problem back"

JOHI:
The advantage of the code above is that you do not use C++ but C.
C is faster on a microcontroller and smaller in footprint in may cases.

I highly doubt that in the case of GCC, at least as a generalization. Probably other compilers too. I would expect a noticeable difference only with advanced OO like polymorphism, etc. Certainly the use of a class like String to perform otherwise simple manipulations will add overhead. Perhaps that is what you actually meant, C utility functions vs. C++ class methods?

Your statement sort of suggests that all C++ features add overhead, I think not. Perhaps that was not your intention, but it's best to avoid misunderstanding. I think many "plain vanilla" C features that were imported with minor changes, into C++, don't add any overhead, execution or memory footprint.

The choice to use, or not use, C++ classes in C++ is indeed an important aspect to consider when the target machine is relatively small and relatively slow.