by

How to Write Binary and Hexadecimal Numbers in Arduino

For an introduction to binary and hexadecimal notation, read this post.

In the Arduino IDE, representing numbers in binary or hexadecimal notation often comes in handy. For example, since the ATmega328’s registers are eight bits, I often set the bits of a register by assigning the register an eight-bit binary number. To assign a variable using binary notation, simply prepend the symbol B to the front of the binary number. For example, the following code assigns the binary equivalent of the number 13 to a variable.

byte myNum = B00001101;

To assign a variable using hexadecimal notation, simply prepend the symbols 0x to the front of the hexadecimal number. For example, the following code assigns the hexadecimal equivalent of the number 255 to a variable.

byte myNum = 0xFF;

Leave a Reply

Your email address will not be published.