Skip to content

more print format constants defined; function printNumber(..) adapted.#271

Open
TobiasKnauss wants to merge 2 commits into
arduino:masterfrom
TobiasKnauss:master
Open

more print format constants defined; function printNumber(..) adapted.#271
TobiasKnauss wants to merge 2 commits into
arduino:masterfrom
TobiasKnauss:master

Conversation

@TobiasKnauss
Copy link
Copy Markdown

I had already created a PR with the same content in arduino/ArduinoCore-avr#621, but I was told that the changes need to be accepted for the API first.

changes:

  • added HEX2, HEX4, HEX8
  • added BIN2, BIN4, BIN8, BIN16, BIN32
  • adapted/enhanced Print::printNumber(unsigned long n, uint16_t base)

The number represents the minimum digit count for printing, i.e. adding leading zeros.

This enhancement was inspired by https://forum.arduino.cc/t/serial-print-value-hex/463868/5

It can be tested with the code example from https://docs.arduino.cc/language-reference/en/functions/communication/serial/print/ enhanced like this:

/*
  Uses a for loop to print numbers in various formats.
*/
void setup()
{
  Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop()
{
  // print labels
  Serial.print("NO FORMAT"); // prints a label 
  Serial.print("\t"); // prints a tab
  Serial.print("DEC"); 
  Serial.print("\t");
  Serial.print("HEX"); 
  Serial.print("\t");
  Serial.print("HEX2"); 
  Serial.print("\t");
  Serial.print("HEX4"); 
  Serial.print("\t");
  Serial.print("HEX8"); 
  Serial.print("\t\t");
  Serial.print("OCT"); 
  Serial.print("\t");
  Serial.print("BIN");
  Serial.print("\t");
  Serial.print("BIN2");
  Serial.print("\t");
  Serial.print("BIN4");
  Serial.print("\t");
  Serial.print("BIN8");
  Serial.print("\t\t");
  Serial.print("BIN16");
  Serial.print("\t\t\t");
  Serial.print("BIN32");
  Serial.println(); // carriage return after the last label
  for (int x = 0; x < 64; x++)
  { // only part of the ASCII chart, change to suit
    // print it out in many formats:
    Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC" 
    Serial.print("\t\t"); // prints two tabs to accomodate the label length
    Serial.print(x, DEC); // print as an ASCII-encoded decimal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, HEX2); // print as an ASCII-encoded hexadecimal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, HEX4); // print as an ASCII-encoded hexadecimal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, HEX8); // print as an ASCII-encoded hexadecimal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, OCT); // print as an ASCII-encoded octal 
    Serial.print("\t"); // prints a tab
    Serial.print(x, BIN); // print as an ASCII-encoded binary 
    Serial.print("\t"); // prints a tab
    Serial.print(x, BIN2); // print as an ASCII-encoded binary 
    Serial.print("\t"); // prints a tab
    Serial.print(x, BIN4); // print as an ASCII-encoded binary 
    Serial.print("\t"); // prints a tab
    Serial.print(x, BIN8); // print as an ASCII-encoded binary 
    Serial.print("\t"); // prints a tab
    Serial.print(x, BIN16); // print as an ASCII-encoded binary 
    Serial.print("\t"); // prints a tab
    Serial.println(x, BIN32); // print as an ASCII-encoded binary 
    // then adds the carriage return with "println"
    delay(200); // delay 200 milliseconds
  }
  Serial.println(); // prints another carriage return
}

- HEX2, HEX4, HEX8
- BIN2, BIN4, BIN8, BIN16, BIN32
The number represents the minimum digit count for printing, i.e. adding leading zeros.
@TobiasKnauss
Copy link
Copy Markdown
Author

Is this failed unit test related to my commit? If I understand it correctly, some external dependencies have failed and this test did not even run.

Added support for printing negative numbers in base-2, -4, -8, and -16 with length of original type and not always with length of uint32.
@TobiasKnauss
Copy link
Copy Markdown
Author

Further improvements:

  • Printing negative numbers in HEX always produced a 8-char-string, even if a byte (u/int8_t) or int (u/int16_t) was provided. The modifications now print in a length that matches the given variable type: byte -> 2-char HEX representation, int -> 4-char HEX representation, etc.

example output attached as file.
output.txt
Serial-print_1.ino.txt (remove ".txt" to run the test program).

TobiasKnauss added a commit to TobiasKnauss/ArduinoLIB_Streaming that referenced this pull request May 29, 2026
…length.

This modification requires changes in print.h and print.cpp of the Arduino Core. The changes are proposed (arduino/ArduinoCore-API#271), but not accepted yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants