Aug 28, 2015

printing binary, octal, duodecimal, hex with arduino

Discovered that the following print optional argument will output integer numbers in various bases. (As of Ver 1.6.5)

Serial.print(nnnn,base)    where nnnn is an integer, and base is an integer,

In addition to base 8 (OCT) and 16 (HEX), it can even do other bases such as 3 or 2  (HEX) or even 7 (should you think of a reason!).

    base = 7;
   Serial.print(millis(),base);
   base = 2;
   Serial.print(millis(),base);

Outputs in base 7 and base 2 respectively

   11514  

   101110111000
 

 Didn't know you could do that.

---
3/15/2016
Addendum: if the first number is a floating point number, then the second argument is the more conventional number of digits after the decimal.

Serial.print(xxx.x,nn)    where xxx.x is a float, and n is an integer,