arduino / arduino/ArduinoCore-avr

Unexpected output from `print` due to transtyping

Open
#347 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

I spotted some unexpected behaviour with the print and println functions.

`char` and `int` with negative value gives unexpected result in `HEX`, `OCT` and `BIN` format due to the `unsigned long` cast before the private `printNumber` method.

Doing so printing a char equal to -128 in `HEX` format gives `FFFFFF80` instead of `80` and gives `11111111111111111111111110000000` instead of `1000000`.

I'm actually looking to a way to improve that but perhaps you came to this behaviour on purpose. I assume `HEX` and `BIN` to be useful to reflect the content of the actual bytes and not only a transcription of its value.

What do you think ?
[bugtest_v2.zip](https://github.com/arduino/ArduinoCore-avr/files/4789116/bugtest_v2.zip)

PS : I came to a solution while writing this, I can share or pull request if you think it worth it.

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the negative char and int cases from bugtest_v2.zip with print and println in HEX, OCT, and BIN formats. Read the implementations of these functions and the private printNumber method, focusing on the unsigned long cast. The issue needs an agreed expected representation for negative values before completion can be verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.