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
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
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
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.