Skip to content

Serial.print as HEX should ALWAYS print 2 chars per byte; it omits leading zeroes [imported] #40

Description

@cmaglie

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.

Describe the current behavior

Upload the following sketch to a board:

void setup() {
  Serial.begin(9600);
  while(!Serial) {}

  Serial.print(0x0F, HEX);
}
void loop() {}

🙁 F is printed in Serial Monitor instead of the expected 0F.

The output for HEX should always be one char per nibble; leading zeroes matter. For example this sketch:

void setup() {
  Serial.begin(9600);
  while(!Serial) {}

  Serial.print("0x");
  Serial.println(0x0F, HEX);
  Serial.println(0x00, HEX);
  Serial.println(0xF0, HEX);
}
void loop() {}

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 ;-)

https://docs.arduino.cc/language-reference/en/functions/communication/serial/print/

Additional requests

Related

Activity

  1. technikadonis commented on Oct 25, 2013

    @technikadonis
  2. darrylp1per commented on Oct 29, 2013

    @darrylp1per

    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.

  3. fake-name commented on Mar 19, 2017

    @fake-name
  4. cmaglie commented on Mar 21, 2017

    @cmaglie
    MemberAuthor

    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?

  5. fake-name commented on Mar 24, 2017

    @fake-name

    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().

  6. clivepengelly commented on Apr 9, 2017

    @clivepengelly
  7. Tijgerd commented on Sep 21, 2017

    @Tijgerd

    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...?

  8. Tijgerd commented on Sep 22, 2017

    @Tijgerd
  9. macsimski commented on Jul 3, 2019

    @macsimski
  10. Tijgerd commented on Jul 3, 2019

    @Tijgerd
  11. JimDrewGH commented on Jul 5, 2020

    @JimDrewGH
  12. linked a pull request that will close this issueAdd Serial.print(foo, HEX_LEADINGZERO) #279on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions