arduino / arduino/ArduinoCore-API

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

Abierto
#40 11 comentarios 0 reacciones 1 asignado Reclamado por @cmaglie Ver en GitHub
enhancement
Lenguaje dominante
C++
Estrellas
306
Forks
150
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.