Home » excel » Large decimal to binary conversion in Excel – formula only (no VBA)

# Large decimal to binary conversion in Excel – formula only (no VBA)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to convert large decimals (up to 2^18) to a formatted binary number.

My decimal numbers begin in cell A3. In B3 I entered

``````=DEC2HEX(\$A3, 5)
``````

to convert to a 5 digit hex number. Then, in C3, I entered

``````= HEX2BIN(MID(\$B3,1,1), 4) & HEX2BIN(MID(\$B3,2,1), 4) & HEX2BIN(MID(\$B3,3,1), 4) & HEX2BIN(MID(\$B3,4,1), 4) & HEX2BIN(MID(\$B3,5,1), 4)
``````

to give me a 20 digit binary number.

Lastly, in D3, I entered

``````=TEXT(\$C3, "0000 0000 0000 0000 0000")
``````

This all works well until I get to 32,769. From then on, I lose the least significant 1!

``````32,763:  0000 0111 1111 1111 1011
32,764:  0000 0111 1111 1111 1100
32,765:  0000 0111 1111 1111 1101
32,766:  0000 0111 1111 1111 1110
32,767:  0000 0111 1111 1111 1111
32,768:  0000 1000 0000 0000 0000
32,769:  0000 1000 0000 0000 0000
32,770:  0000 1000 0000 0000 0010
32,771:  0000 1000 0000 0000 0010
32,772:  0000 1000 0000 0000 0100
32,773:  0000 1000 0000 0000 0100
32,774:  0000 1000 0000 0000 0110
32,775:  0000 1000 0000 0000 0110
32,776:  0000 1000 0000 0000 1000
32,767:  0000 1000 0000 0000 1000
``````

Why???

I have a workaround (which is a pain in the neck because my format is really more like “00 000 000 0-00 000 000-0”):

``````= TEXT(HEX2BIN(MID(\$B3,1,1), 4), "0000") & " " & TEXT(HEX2BIN(MID(\$B3,2,1), 4), "0000") & " "  & TEXT(HEX2BIN(MID(\$B3,3,1), 4), "0000") & " "  & TEXT(HEX2BIN(MID(\$B3,4,1), 4), "0000") & " "  & TEXT(HEX2BIN(MID(\$B3,5,1), 4), "0000")
``````

But why isn’t the original working?

How to&Answers:

You cannot format a 20 digit number using the TEXT function since Excel’s precision is limited to 15 digits. Anything longer will become a zero.

You need to use string functions to obtain your desired formatting, but you just need to split your string in `C` into small enough (less than 15 digit) strings.

``````=TEXT(LEFT(C3,8),"0000 0000 ") & TEXT(MID(C3,9,12),"0000 0000 0000")
``````

You can do something similar for your desired formatting, but what you posted only comprises 18 digits, so you need to use the correct format.

### Answer：

Your formula in C3 takes the separate 4-digit binary numbers and concatenates them to form a single long string. Why not just insert gaps at that point instead:

`= HEX2BIN(MID(\$B3,1,1), 4) & " " & HEX2BIN(MID(\$B3,2,1), 4) & " " &HEX2BIN(MID(\$B3,3,1), 4) & " " & HEX2BIN(MID(\$B3,4,1), 4) & " " & HEX2BIN(MID(\$B3,5,1), 4)`

Or you can do it using an array formula (in Excel 2016):

`=TEXTJOIN(" ",FALSE,HEX2BIN(MID(B3,ROW(\$1:\$5),1),4))`