HEX2BIN function
The HEX2BIN function in Excel is used to convert a hexadecimal number (base 16) to its binary equivalent (base 2).
Syntax
HEX2BIN(number, [places])
Parameters
number: The hexadecimal number that you want to convert to binary. This can be a string or a cell reference containing the hexadecimal number.[places](optional): The number of characters to use for the resulting binary number. If omitted, Excel returns the smallest number of binary digits necessary to represent the hexadecimal number. If the result has fewer digits than the specified number of places, Excel pads the result with leading zeros.
How It Works
The HEX2BIN function converts a hexadecimal (base 16) number into a binary (base 2) number. A hexadecimal number is composed of digits 0-9 and A-F, where A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15 in decimal. Excel converts the hexadecimal digits into their binary equivalents.
Examples
- Basic Conversion: To convert the hexadecimal number
A(which is 10 in decimal) into binary:=HEX2BIN("A")The result will be “1010” in binary.
- Hexadecimal Number with Leading Zeros: To convert the hexadecimal number
3F(which is 63 in decimal) into binary:=HEX2BIN("3F")The result will be “111111” in binary.
- Specifying Places: To convert the hexadecimal number
Ato binary and ensure the result is at least 8 digits long (padded with leading zeros):=HEX2BIN("A", 8)The result will be “00001010”, with leading zeros added to make it 8 digits long.
- Using Cell Reference: If cell
A1contains the hexadecimal value1F, you can convert it to binary:=HEX2BIN(A1)The result will be “11111” (binary representation of 31 in decimal).
- Negative Hexadecimal Number: The HEX2BIN function can also handle negative hexadecimal numbers. For example, to convert
-1to binary:=HEX2BIN("-1")The result will be “11111111111111111111111111111111”, which is the 32-bit two’s complement binary representation of
-1.
Important Notes
- The HEX2BIN function handles both positive and negative hexadecimal numbers. For negative values, it uses two’s complement representation in 32-bit format, which may result in a 32-character binary string.
- The result of HEX2BIN will always be a string, not a number, even though the string consists of binary digits (0s and 1s).
- If the number contains invalid hexadecimal characters (i.e., characters not in the range 0-9 and A-F), Excel will return an error.
Summary
The HEX2BIN function in Excel is used to convert hexadecimal numbers into binary numbers. It can be useful for converting base 16 to base 2 in various applications, including when working with computer systems or performing bitwise operations. The function also allows you to specify the number of binary digits for the result.