Arduino : Convert Decimal to Binary

arduino convert decimal to binary

When programming Arduino you’ll eventually at some point want to get to converting decimals to binary or the other way around. In most cases it’s normally because you want to read a value and set a number of pins on the board or use a shift register etc… to control multiple pins e.g. a PWM value from one pin (0-255) that then sets 8 pins to either high or low (on or off) depending upon it’s representation in binary, 255 = 11111111 setting all 8 pins to high or 1 = 00000001 setting only one pin to high.

For the purposes of this we’re going to consider only using decimals up to 8 bits/ 1 byte, if you were going for an integer larger than 255 then you’d use 16 bits for representation in binary which Arduino can handle just fine – 8 bits just means it’s slightly easier for me to write up.

I could show the really, really long way of doing this where you iterate over each digit of a decimal and then convert that into a binary after one giant for loop but why bother when there’s a much quicker way using Arduinos / C++ inbuilt functions that are available to you for free.

Here’s the code, just copy, paste upload to your Arduino board and check the serial monitor:

void setup()

void loop()
int myNum = 67;
int zeros = 8 - String(myNum,BIN).length();
String myStr;
for (int i=0; i

This is only an example so I've left all my variables inside the loop. The setup loop just sets the serial port monitor so you can see an output.

So first we declare our decimal to convert, in the real world this would be a value fetched from the serial port or from a sensor of some kind. You'd also want to check that the number is less or equal to 255 and is not a negative nor a float (meaning it has a decimal point) e.g. if myNum >=255 then do my stuff etc... We declare our decimal as an integer (a whole number).

Now skipping slightly ahead you'll see that I'm using String(myNum, BIN). This is a built in function of the String object in Arduino that, for free, will convert your number into a binary string, you can also use DEC, HEX and OCT for other conversions (guess what DEC does?). The trouble with this is that it misses the leading/preceeding zeros off the conversion, so 1 = 1 not 00000001 and while it's not the most efficient way to add in the zeros it does mean later on when you loop through that value you can always expect 8 bits and don't have to allow for it to be more or less.

To add in the zeros we can use another function of the String object and calculate its length - how many characters make up the string. We then subtract this number from 8 to get how many zeros we need to add and with a simple for loop we create our string with the required zeros.

We then join / concatenate our string with the original binary conversion to get our 8 bit string and that's it. You can write the string joins in a shorter way but for understanding the code I've ignored this.

So now we have a binary string that can be sent over serial or then split up to turn on and off 8 pins. You'd probably want to convert this string to a character array, char[], and iterate over that array to then set/unset your pins.


  • So a function like so should return a string of the byte ‘value’ with leading zeros than?

    String addLeadingZeros(byte value)
    int zeros = 8 – String(value,BIN).length();
    String returnValue = “”;
    for (int i = 0; i < zeros; i++) {
    returnValue += "0";
    returnValue += String(value, BIN);
    return returnValue;

    Also, does the String class let you concatenate them into a single string later? Say I convert four bytes into strings with leading zeros and I want to concatenate all of them into one 32 bit String can I just do something like…

    dataString += firstByte;
    dataString += secondByte;
    dataString += thirdByte;
    dataString += fourthByte;

    and then print dataString out as a string or iterate over it or whatever?

    Thanks for your help.

  • Hi….pleeeeaseeee….could you help with a code to convert a decimal number to a hex number???

    Thanks !!!!

You must be logged in to post a comment.