# Convert and pad to two digits \<\< SOLVED\>\>

**URL:** <https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904>\
**Category:** Programming\
**Created:** [January 29, 2022, 4:12pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904 "2022-01-29T16:12:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dolittle](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@dolittle](https://forum.arduino.cc/u/dolittle)\
**Post date:** [January 29, 2022, 4:12pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/1 "2022-01-29T16:12:25Z")

</div>

I am using a keypad with IR receiver - the value when a key is pressed (0)=0x16.(1)=0xC etc  
I wish to pad the input to two places and have it shown as 00, 01 - 99  
Can anyone help with the coding please.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 29, 2022, 4:15pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/2 "2022-01-29T16:15:28Z")

</div>

```cpp
    if (value < 10)
      Serial.print('0'); // Add leading zero
    Serial.print(value);
```

---

<div class="post-metadata">

**Author:** ![dolittle](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@dolittle](https://forum.arduino.cc/u/dolittle)\
**Post date:** [January 29, 2022, 4:19pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/3 "2022-01-29T16:19:08Z")

</div>

Thanks for the quick reply - I am not sure how that would convert and pad value.  
could you give me a clearer idea.

if value = 0xC then result= ????

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 29, 2022, 4:23pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/4 "2022-01-29T16:23:58Z")

</div>

> [@dolittle](#):
>
> if value = 0xC then result= ????

it’s Hex representation of …

0123456789AB **C** DEF …

**12**

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [January 29, 2022, 4:29pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/5 "2022-01-29T16:29:35Z")

</div>

@dolittle, the idea is not to change the variable itself, but only add the zero when displaying that value.

This is what johnwasser did, I put both decimal and hexadecimal in there:

```cpp
void setup() 
{
  Serial.begin( 115200);
}

void loop() 
{
  int key = random( 0, 25);

  // ------------------------------------------------
  // Print as decimal
  // ------------------------------------------------
  // Value is 10 or above ? No leading zero
  // Value is 9 or below ? Add leading zero
  if( key < 10)
  {
    Serial.print( "0"); // Add leading zero
  }
  Serial.print( key, DEC);

  // ------------------------------------------------
  // Print as hexadecimal
  // ------------------------------------------------
  // Value is 0x10 or above ? No leading zero
  // Value is 0x9 or below ? Add leading zero
  Serial.print( " 0x");
  if( key < 0x10)
  {
    Serial.print( "0");
  }
  Serial.print( key, HEX);
  Serial.println();

  delay( 500);
}

```

Try this sketch in Wokwi:

> **[LeadingZero.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/322138036689175122)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

[ADDED] Updated the sketch in Wokwi. Added the printf() as shown by @groundFungus but made it safer.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [January 29, 2022, 4:32pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/6 "2022-01-29T16:32:53Z")

</div>

Using [the sprintf() function.](https://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf)

```arduino
int numberA = 7;
int numberB = 0xc;

void setup()
{
    Serial.begin(115200);
    
    char numAstr[5];
    char numBstr[5];
    
    sprintf(numAstr,"%02d", numberA);
    Serial.print("number A = ");
    Serial.print("0X");
    Serial.println(numAstr);
    
    sprintf(numBstr,"%02X", numberB);
    Serial.print("number B = ");
    Serial.print("0X");
    Serial.println(numBstr);   
}

void loop()
{

}

```

Output:

> number A = 0X07  
> number B = 0X0C

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 29, 2022, 4:32pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/7 "2022-01-29T16:32:58Z")

</div>

> [@dolittle](#):
>
> if value = 0xC then result= ????

It was not clear that you wanted the result in hexadecimal.

```arduino
    if (value < 0x10)
      Serial.print('0'); // Add leading zero
    Serial.print(value, HEX);

```

---

<div class="post-metadata">

**Author:** ![dolittle](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@dolittle](https://forum.arduino.cc/u/dolittle)\
**Post date:** [January 29, 2022, 4:46pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/8 "2022-01-29T16:46:48Z")

</div>

Sorry for not explaining myself better - something along this line: I know its not correct but this is what I am looking for

```cpp
String doConvert(HEX value) {
  string result ;

  if (value < 0x10)
    result = '0'+ 'what';
  else
    result =' what';

      return result;
}

```

The return should be two characters 01,02,03,04 etc

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 29, 2022, 4:53pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/9 "2022-01-29T16:53:32Z")

</div>

```cpp
String doConvert(int value)
{
  String result ;

  if (value < 0x10)
    result = '0' + String(value, HEX);
  else
    result = String(value, HEX);

  return result;
}

```

---

<div class="post-metadata">

**Author:** ![dolittle](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@dolittle](https://forum.arduino.cc/u/dolittle)\
**Post date:** [January 29, 2022, 5:16pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/10 "2022-01-29T17:16:00Z")

</div>

perfect - thank you

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [January 29, 2022, 5:55pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/12 "2022-01-29T17:55:04Z")

</div>

Nice to see you came up with own solution

---

<div class="post-metadata">

**Author:** ![dolittle](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@dolittle](https://forum.arduino.cc/u/dolittle)\
**Post date:** [January 29, 2022, 8:03pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/13 "2022-01-29T20:03:47Z")

</div>

Thanks for pointing that out, seems to be floppy fingers.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 28, 2022, 8:04pm UTC](https://forum.arduino.cc/t/convert-and-pad-to-two-digits-solved/952904/14 "2022-07-28T20:04:02Z")

</div>

Closed on July 28, 2022, 8:04pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
