arduino / arduino/ArduinoCore-API

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

Đang mở
#40 11 bình luận 0 reaction 1 người được giao Được @cmaglie nhận Xem trên GitHub
enhancement
Ngôn ngữ chính
C++
Star
306
Fork
150
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.