You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Serial.print as HEX should ALWAYS print 2 chars per byte; it omits leading zeroes [imported] #40
This is Issue 1097 moved from a Google Code project.
Added by 2012-11-05T10:22:49.000Z by dale.cal...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Describe the request
Ensure Print class function output always has two characters per byte when HEX base is used.
Will output 0xF0FO instead of the intended 0x0F00FO. In this example, the value is totally changed by omission of the leading zero.
arduino/ArduinoCore-API version
Arduino IDE 1.0.1, Duemilanove
Additional context
I can't think of any reason to omit leading zeroes when using the HEX format. In cases like this, where a sequence of bytes is read from a device that represents a multi-byte return value, and you want to see the real value returned it gives incorrect results. One could assemble the bytes into a LONG and then call the function, but I think that most who use the HEX format would like to see all their nibble values with any size data.
The example in the Arduino Language Reference sidesteps the issue by not using a value that would require a leading zero ;-)
Changing the default behaviour of Print(..., HEX) is a backward incompatible change, it will suddenly break all existing sketches that relies on the current behaviour of Print (i.e. to print just the HEX value without leading zeros). Just this argument is enough to mark this issue as wont-fix.
Moreover, printing the leading zeros may be good for some kind of applications but not for others... what if I really want to print just the single hex number without the leading zero?
if HEX is an ENUM
can't they just ad: HEX_LEADINGZERO to the enum and make every call to Serial.print(hexVal,HEX_LEADINGZERO) return a HEX format with leading zero...?
Activity
technikadonis commentedon Oct 25, 2013
darrylp1per commentedon Oct 29, 2013
the thread on http://forum.arduino.cc//index.php?topic=179111.0 has some idears for this call
in particular, the changes I listed here
http://forum.arduino.cc//index.php?topic=179111.msg1385295#msg1385295
based on my altered code found last posted here
http://forum.arduino.cc//index.php?topic=179111.msg1334253#msg1334253
gives you the insight to a working solution, that fits in easily.
fake-name commentedon Mar 19, 2017
cmaglie commentedon Mar 21, 2017
Changing the default behaviour of Print(..., HEX) is a backward incompatible change, it will suddenly break all existing sketches that relies on the current behaviour of Print (i.e. to print just the HEX value without leading zeros). Just this argument is enough to mark this issue as wont-fix.
Moreover, printing the leading zeros may be good for some kind of applications but not for others... what if I really want to print just the single hex number without the leading zero?
fake-name commentedon Mar 24, 2017
Printing a hex byte without the leading zero doesn't even make sense.
If you want that, you want a print nybble function.
At minimum, there needs to be a way to format hex data in the correct manner, right now we're stuck with an intermediate
snprintf().clivepengelly commentedon Apr 9, 2017
Tijgerd commentedon Sep 21, 2017
if HEX is an ENUM
can't they just ad: HEX_LEADINGZERO to the enum and make every call to Serial.print(hexVal,HEX_LEADINGZERO) return a HEX format with leading zero...?
Tijgerd commentedon Sep 22, 2017
arduino/Arduino#6750
macsimski commentedon Jul 3, 2019
Tijgerd commentedon Jul 3, 2019
JimDrewGH commentedon Jul 5, 2020