ZetCode

Excel DEC2BIN and HEX2DEC Functions

last modified October 5, 2026

The DEC2BIN and HEX2DEC functions convert numbers between the decimal system that people read and the binary and hexadecimal systems that computers use. DEC2BIN turns a decimal integer into a binary string, while HEX2DEC turns a hexadecimal string back into a decimal number. This tutorial covers their syntax, argument ranges, two's-complement representation of negative values, the places argument, and detailed examples.

DEC2BIN and HEX2DEC Function Basics

DEC2BIN converts a decimal number to binary, and HEX2DEC converts a hexadecimal number to decimal. DEC2BIN returns text, while HEX2DEC returns a number that can be used directly in calculations.

Function Description Syntax
DEC2BIN Converts a decimal integer to a binary string =DEC2BIN(number, [places])
HEX2DEC Converts a hexadecimal string to a decimal number =HEX2DEC(number)

DEC2BIN accepts an optional places argument that pads the result with leading zeros, while HEX2DEC accepts a single argument.

Argument Meaning
number For DEC2BIN, the decimal integer to convert, from -512 to 511. For HEX2DEC, a hexadecimal value of up to 10 characters (40 bits).
places Optional. The minimum number of characters in the binary result. It is ignored when the number is negative. If the result needs more characters than places, Excel returns #NUM!.

How Two's Complement Works

Excel does not use a minus sign in the binary or hexadecimal result. Negative numbers are written in two's complement, a scheme in which the leftmost bit is the sign bit. A leading 1 means the value is negative, and the remaining bits hold the magnitude in complemented form.

Value 10-bit binary 40-bit hexadecimal
1 0000000001 0000000001
-1 1111111111 FFFFFFFFFF
-2 1111111110 FFFFFFFFFE
-5 1111111011 FFFFFFFFFB
-512 1000000000 FFFFFFFE00

You can check any negative value by adding 2 raised to the number of bits. For a 10-bit binary result, -5 becomes 1024 - 5 = 1019, which is 1111111011. For a 40-bit hexadecimal result, -5 becomes 2^40 - 5 = 1,099,511,627,771, or FFFFFFFFFB. Note that DEC2BIN only produces the padded 10-bit form for negative numbers; positive values use the minimum number of digits unless places is given.

Basic DEC2BIN Example

This example demonstrates the simplest use of DEC2BIN.

Basic DEC2BIN formula
=DEC2BIN(10)

The formula converts decimal 10 to binary and returns the text "1010". Because places is omitted, the function uses the minimum number of characters needed. The result is text, so it is left-aligned by default and cannot be used directly in arithmetic.

DEC2BIN with the Places Argument

The optional places argument sets the minimum number of characters in the result. Excel pads the binary value with leading zeros until it reaches that length.

DEC2BIN with places
=DEC2BIN(5, 8)      ' "00000101"
=DEC2BIN(9, 4)      ' "1001"
=DEC2BIN(9, 3)      ' #NUM!, 1001 needs 4 characters

The first formula pads decimal 5 to eight characters, producing "00000101". The second already has four digits, so places adds nothing. The third asks for only three characters but the result needs four, so DEC2BIN returns #NUM!. A non-integer places value is truncated toward zero, and zero or a negative value returns #NUM!.

Basic HEX2DEC Example

This example shows the basic usage of HEX2DEC.

Basic HEX2DEC formula
=HEX2DEC("A")

The formula converts the hexadecimal digit "A" to its decimal value and returns 10. Hexadecimal uses the digits 0-9 and the letters A-F, and HEX2DEC accepts them in either case. The argument is normally supplied as text, so it is wrapped in quotation marks.

HEX2DEC with Multi-digit Hexadecimal

HEX2DEC handles values of up to 10 hexadecimal digits, which is 40 bits.

HEX2DEC with multi-digit hex
=HEX2DEC("A5")          ' 165
=HEX2DEC("FF")          ' 255
=HEX2DEC("3DA408B9")    ' 1034160313

"FF" is the largest 8-bit value, 255. "A5" is 10*16 + 5 = 165. The eight-digit "3DA408B9" equals 1,034,160,313. Because the leftmost bit of "3DA408B9" is 0, the value is positive.

Negative Numbers in DEC2BIN

When the number is negative, DEC2BIN ignores places and always returns a 10-character two's-complement string. The leftmost bit is the sign bit and the remaining nine bits are the magnitude.

DEC2BIN with negative numbers
=DEC2BIN(-1)        ' "1111111111"
=DEC2BIN(-5)        ' "1111111011"
=DEC2BIN(-512)      ' "1000000000"

The result always has exactly 10 characters, so =DEC2BIN(-1, 4) still returns "1111111111" and not a 4-character string. To read a negative two's-complement value back, use BIN2DEC.

Negative Numbers in HEX2DEC

A hexadecimal value of up to 10 digits can also be negative. When the 40th bit (the leftmost bit of a 10-digit value) is 1, HEX2DEC interprets the string as a negative two's-complement number.

HEX2DEC with negative results
=HEX2DEC("FFFFFFFFFF")      ' -1
=HEX2DEC("FFFFFFFF00")      ' -256
=HEX2DEC("8000000000")      ' -549755813888

Ten F digits represent -1, "FFFFFFFF00" represents -256, and "8000000000" is the most negative 40-bit value, -549,755,813,888. The value is computed as the unsigned 40-bit number minus 2^40 whenever the sign bit is set.

Range Limits

Both functions accept only a fixed window of values. Out-of-range input produces #NUM! rather than an approximate result.

Function Accepted input Out of range
DEC2BIN Integers from -512 to 511 #NUM!
HEX2DEC Up to 10 hex digits (40 bits) #NUM!
Range limits
=DEC2BIN(511)       ' "111111111", the largest positive value
=DEC2BIN(512)       ' #NUM!
=DEC2BIN(-512)      ' "1000000000", the smallest value
=DEC2BIN(-513)      ' #NUM!

The largest positive decimal is 511, which becomes the nine-bit "111111111". The smallest is -512, the ten-bit "1000000000". Values just outside the window, such as 512 and -513, return #NUM!. Fractional input is truncated toward zero before conversion, so =DEC2BIN(9.9) returns "1001", the same as =DEC2BIN(9).

Combining DEC2BIN and HEX2DEC

The two functions can be nested to convert hexadecimal to binary, because DEC2BIN accepts the decimal number that HEX2DEC produces.

Hex to binary conversion
=DEC2BIN(HEX2DEC("A5"))     ' "10100101"

HEX2DEC("A5") returns 165, and DEC2BIN(165) returns "10100101". This only works while the intermediate decimal value stays inside the -512 to 511 range of DEC2BIN. For larger values, use HEX2BIN, which converts directly.

Related Conversion Functions

Excel groups DEC2BIN and HEX2DEC with a family of engineering functions that convert between number systems.

Function Description Example Result
DEC2HEX Decimal to hexadecimal =DEC2HEX(100, 4) 0064
DEC2HEX Negative decimal to 40-bit hex =DEC2HEX(-54) FFFFFFFFCA
BIN2DEC Binary to decimal =BIN2DEC("1100100") 100
BIN2DEC Negative 10-bit binary to decimal =BIN2DEC("1111111111") -1
HEX2BIN Hexadecimal to binary =HEX2BIN("F", 8) 00001111

DEC2HEX works like DEC2BIN but with a much wider range, -549,755,813,888 to 549,755,813,887, and produces up to 10 hexadecimal characters. BIN2DEC is the inverse of DEC2BIN and reads a 10-bit binary string. HEX2BIN converts directly, but its result is still limited to 10 bits, so its input must lie between FFFFFFFE00 and 1FF.

Practical Application Example

Converting the octets of an IPv4 address to binary is a common task. Each octet is a value from 0 to 255 that is displayed as exactly eight bits.

IP address component conversion
=DEC2BIN(192, 8)        ' "11000000"
=DEC2BIN(168, 8)        ' "10101000"

The places argument forces each octet to eight characters, so the bits line up when you join them. Without places, a small octet such as 1 would lose its leading zeros and the address would be ambiguous.

DEC2BIN and HEX2DEC Errors and Troubleshooting

Cause Symptom Fix
DEC2BIN number is below -512 or above 511 #NUM! error Keep the value inside -512 to 511, or use DEC2HEX for a wider range
The result needs more characters than places #NUM! error Increase places or omit it so Excel uses the minimum length
places is zero or negative #NUM! error Use a positive integer for places
DEC2BIN number is non-numeric text #VALUE! error Convert the text with VALUE or correct the source cell
places is non-numeric text #VALUE! error Supply a number or omit the argument
HEX2DEC argument is not valid hexadecimal #NUM! error Use only the digits 0-9 and letters A-F, up to 10 characters
HEX2DEC argument has more than 10 characters #NUM! error Shorten the value to at most 40 bits
A referenced cell holds an error The same error is returned Fix the upstream error; the conversion cannot mask it

The DEC2BIN and HEX2DEC functions bridge the decimal world of the spreadsheet and the binary and hexadecimal world of computers. DEC2BIN converts integers from -512 to 511 and, for negative values, returns a fixed 10-bit two's-complement string; its optional places argument controls leading-zero padding. HEX2DEC reads up to 10 hexadecimal digits and treats the 40th bit as a sign bit, so the same two's-complement rule covers its negative results. Remember that both return #NUM! for out-of-range or malformed input, and that nesting them lets you move between any two of the three number systems.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Excel Formulas.