String Convert & print Problem

Hi

I have one problem with convert & print.

Here is little example with code.

String gpsTime = "";
byte hour = 05;
byte mint = 10;
byte sec = = 09;

   gpsTime.concat(hour);
      gpsTime.concat(mint);
      gpsTime.concat(sec);
      Serial.println(gpsTime);

When serial print execute, it prints "5109"
i want print result like this "051009"

How can i achieve this.

Need Help.

Thanks

Hi,

I want to refine my problem.

Actually i want to look leading 000 in my string.

if byte1 = 5, byte2 = 6

& i want to add both in one string then result should be 0506

Thanks

Hi BharatSun,

try this. :slight_smile:

  void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
  
    String gpsTime = "";
    byte h = 5;
    byte m = 10;
    byte s = 9;

    gpsTime += (h < 10) ? ("0" + (String)h) : ((String)h);  //hour formatting
    gpsTime += ":";
    gpsTime += (m < 10) ? ("0" + (String)m) : ((String)m);  //minute formatting
    gpsTime += ":";
    gpsTime += (s < 10) ? ("0" + (String)s) : ((String)s);  //second formatting
    Serial.println(gpsTime);
  }

  void loop() {
    // put your main code here, to run repeatedly:
  
  }

i hope it will help.

Hi!

Its works perfect.

thanks

BTW will you please tell me where i can find ref for writing if then statement using ? as you write.

thanks

BharatSun: BTW will you please tell me where i can find ref for writing if then statement using ? as you write.

I have a short article about it here: What does this code do? a = x ? y : z;

In all my years of coding, those never made any sense.

-jim lee

that's great!

BharatSun: BTW will you please tell me where i can find ref for writing if then statement using ? as you write.

you can try this link below, thanks to the author of this page. :) https://www.tutorialspoint.com/cprogramming/c_decision_making.htm