Finding a leading zero

Apart from using characters, is it possible to make a byte in HEX to show a leading 0?. I mean i.e. from zero to 0F I need to print 00..01....etc...0F. Thank you in advance.

Yes, it would be "%02X"

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Another job for sprintf(). Something like:

char buf[8];
sprintf(buf, "%04x", my_value);
Serial.println(buf);

Thank you guix and gardner. I fixed my issue using:

char buf[8];
sprintf(buf, "%02x", my_value);
Serial.println(buf);

No problem but use an upper case X if you want values such as "0F" instead of "0f", and buf[3] is enough, don't waste precious memory :slight_smile:

if (x < 16) {
  Serial.print ("0");
}
Serial.print (x, HEX);