Serial print leading zeroes

Status
Not open for further replies.

TelephoneBill

Well-known member
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.
 
For that I use Serial.printf.
Example: Serial.printf("%08x", Byte1);

Will print 8 characters wide with 0s filling...
 
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 "
 
Status
Not open for further replies.
Back
Top