void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
char buff[5];
int hour = 5;
int minute = 15;
int second = 5;
PadValue(buff, hour);
Serial.print(buff);
Serial.print(":");
PadValue(buff, minute);
Serial.print(buff);
Serial.print(":");
PadValue(buff, second);
Serial.print(buff);
Serial.println();
}
void PadValue(char str[], int num)
{
itoa(num, str, 10);
if (strlen(str) == 1) {
str[2] = '\0';
str[1] = str[0];
str[0] = '0';
}
}
void loop() {
// put your main code here, to run repeatedly:
}
PaulS's code needs a "%" added before the last conversion expression (e.g., "%2d"). After that change and using the Serial monitor as output, the code takes 3276 bytes. The version above uses 2066 bytes. A good part of the difference is because sprintf() is such a powerful and flexible function it often has more features than one program uses. It's the old H-bomb-to-kill-an-ant issue. While the source code "looks" like more code, the compiled code is considerably less.