Number System Transformation


As you will know, the Serial.print command accepts different types of variables and, in case of integer types, it offers to print the numbers not only in the decimal format but also in the binary system, the octal system, and the hexadecimal system by adding a second parameter BIN, OCT, HEX, or DEC (default).

Those parameters are nothing else than the numerical constants 2, 8, 16, and 10.

Will you be surprised that no error occurs when you code:

void setup() {
  Serial.begin(9600);
  Serial.print(101,7);
}

void loop() {}

The output will be 203 meaning 2037 equal to 2 x 72 + 0 x 71 + 3 x 70.

It also works for base values above 16:

void setup() {
  Serial.begin(9600);
  Serial.print(101,17);
}

void loop() {}

giving you the output 5G meaning 5G17 equal to 5 x 171 + 16 x 70 = 85 + 16.

This only applies when the first argument in print/println is an integer/byte/long. If it is float, the second argument gives the number of decimals to be printed.

P.S.: There are of course more constants defined in the Arduino system, e.g.:

INPUT = 0
OUTPUT = 1




contact: nji(at)gmx.de