Skip to content

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

Open
TobiasKnauss wants to merge 1 commit intoarduino:masterfrom
TobiasKnauss:master
Open

more print format constants defined; function printNumber(..) adapted.#271
TobiasKnauss wants to merge 1 commit intoarduino: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.

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