Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Serial print leading zeroes

  1. #1
    Senior Member
    Join Date
    Mar 2015
    Location
    UK
    Posts
    159

    Serial print leading zeroes

    Is there a simple way to force the printing of leading zeroes when using "Serial.print(Byte1,HEX)" where Byte1 = 0x01?

    I'm printing here to the SERIAL MONITOR. I'm wanting to print several bytes consecutively (Bytes 1 to 8), and leading zeros would help me visually parse the string.

  2. #2
    Moderator KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,141
    For that I use Serial.printf.
    Example: Serial.printf("%08x", Byte1);

    Will print 8 characters wide with 0s filling...

  3. #3
    Another couple of examples to work from

    Serial.printf("%02X ",Byte1); // prints "01 " ie the hex value with a trailing space
    Serial.printf("%02X %02X %02X %02X %02X %02X %02X %02X ",Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 ,Byte7, Byte8); // Prints "01 02 03 04 05 06 07 08 "
    Serial.printf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X ",Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 ,Byte7, Byte8); // Prints "0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 "

  4. #4
    Senior Member
    Join Date
    Mar 2015
    Location
    UK
    Posts
    159
    Thank you both - very helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •