Go Down

Topic: Finding a leading zero (Read 815 times) previous topic - next topic

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.

guix

Yes, it would be "%02X"

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

gardner

Another job for sprintf().  Something like:

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

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

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

guix

#4
Oct 11, 2012, 05:12 am Last Edit: Oct 11, 2012, 05:24 am by guix Reason: 1
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 :)

AWOL

Code: [Select]
if (x < 16) {
  Serial.print ("0");
}
Serial.print (x, HEX);
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy