arduino / arduino/ArduinoCore-API

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

Offen
#40 11 Kommentare 0 Reaktionen 1 zugewiesene Person Beansprucht von @cmaglie Auf GitHub ansehen
enhancement
Vorherrschende Sprache
C++
Sterne
306
Forks
150
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

This is [Issue 1097](http://code.google.com/p/arduino/issues/detail?id=1097) moved from a Google Code project.
Added by 2012-11-05T10:22:49.000Z by [dale.cal...@gmail.com](http://code.google.com/u/116890775079754811516/).
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:

```cpp
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:

```cpp
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

- https://github.com/arduino/ArduinoCore-API/issues/40#issuecomment-27121106
- https://github.com/arduino/ArduinoCore-API/issues/40#issuecomment-287592499
- https://github.com/arduino/ArduinoCore-API/issues/40#issuecomment-292790749
- https://github.com/arduino/ArduinoCore-API/issues/40#issuecomment-508018244
- https://github.com/arduino/ArduinoCore-API/issues/40#issuecomment-653824858

#### Related

- https://github.com/arduino/Arduino/pull/6750

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.