Convert int to sring with leading zero

I am reading two intergers representing minutes and hours from the EEPROM on an ESP32 and converting them to a string with String() to display on a weppage. Unfortunately the result is missing the leading zero for numbers less than 10 and so the result is displayed as something like 2:5 when I would prefer 02:05.

Obviously I could easily use code to add the leading zero to the strings when required, but I was wondering if there is a way to format the result of the String() function to do this automatically.

The first thing that I note is that you seem to be referring to Strings (uppercase S) which are objects of the String library and strings (lowercase s) which are zero terminated arrays of chars. The ESP32 has lots of memory so using Strings may not be a problem but generally strings are to be preferred

I know little or nothing about Strings so would do it this way

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  int hour = 2;
  int minute = 5;
  printTime(hour, minute);
}

void loop()
{
}

void printTime(int h, int m)
{
  char buffer[6];
  sprintf(buffer, "0%d:0%d", h, m);
  Serial.println(buffer);
}

I do not seem to have explained myself very well. I am not trying to write anything at all to the Serial Monitor. I want to convert a single digit integer to a two digit string for further manipulation. That is I want to convert the number 5 to the string "05".

I am not trying to write anything at all to the Serial Monitor

Then don't write it to the Serial monitor. My sketch was an illustration not an answer to a question that you had not asked. You said that you wanted 02:05 and that is what you got.

Actually the program has a bug (or 2) in it and should be

  sprintf(buffer, "%02d:%02d", h, m);

UKHeliBob:
[...] Strings (uppercase S) which are objects of the String library and strings (lowercase s) which are zero terminated arrays of chars.

A veteran member of this Forum has said in a recent post in some other thread that String is a Class which encapsulates various text manipulating methods. If so, objects could be created from this Class (I have used capital C for Class in this case to distinguish it from the keyword class). For example: String myString;. Here, myString is an object of type String.

Sorry GM but I have no idea what you are talking about and how it relates to what I wrote or the subject of this topic

String myString;. Here, myString is an object of type String.

Correct. So what ?

I have wished to convey the message that the anonymous veteran has termed the String as a Class and you have termed it as an Object. Are Class and Object same? Can we apply methods on Class?

Are Class and Object same?

No

For example there is a Servo class in the Servo library and when you do

Servo servo1;
Servo servo2;

You create 2 instances of Servo objects each with their own characteristics

"I want to convert a single digit integer to a two digit string for further manipulation. That is I want to convert the number 5 to the string "05"."

Well, you convert the number 5 to the ascii visual representation of the number 5. What you do after that is just creating a visual do-dad representation of the original number, like 05 or 000005.

GolamMostafa:
I have wished to convey the message that the anonymous veteran has termed the String as a Class and you have termed it as an Object. Are Class and Object same? Can we apply methods on Class?

An object is simply an instance of a class.

GolamMostafa:
I have wished to convey the message that the anonymous veteran has termed the String as a Class and you have termed it as an Object. Are Class and Object same? Can we apply methods on Class?

A Class is like a set of blueprints...it tells you the attributes something has, like 2 stories, a roof, 3 bedrooms, etc. However, you cannot "move into" a set of blueprints; the house needs to be built. More formally, a Class is a declaration of what the class should look like (attributes) and the processes (methods) it is capable of.

An object is an instantiation of a class. Memory is allocated for the object and, once instantiated, you can use the object in your program. Usually, a header file (.h) provides the declaration of what the object looks like and its associated source file (.cpp) details how the methods are built.

So if you want to use the Servo library, you #include <Servo.h> which also uses Servo.cpp. Then your code:

Servo myServo1;

defines a Servo object of Servo class and gives it the name myServo1. It's myServo1 that gets manipulated in your program.

is this what you are looking for?

int hour = 5;
int mini = 2;
String s = String(hour)+":";
if(mini<10){s=s+"0";}
s=s+String(mini);
Serial.println(s);

taterking:
is this what you are looking for?

int hour = 5;

int mini = 2;
String s = String(hour)+":";
if(mini<10){s=s+"0";}
s=s+String(mini);
Serial.println(s);

No, s/he's looking for a sring.
And s/he doesn't want to print it.

No, s/he's looking for a sring.
And s/he doesn't want to print it.

and he/she would prefer not to write any code to do it

"No, s/he's looking for a sring.
And s/he doesn't want to print it."

It will be "printed" to a web page per the The OP's statement, so the String code posted looks functional.