To convert a hexadecimal number to binary in Python, you can follow these steps:
- Convert the hexadecimal number to an integer.
- Convert the integer to binary.
Method 1: Using bin()
function
The easiest way to convert a hexadecimal string to binary is by first converting the hexadecimal number to an integer using int()
, and then converting that integer to a binary string using the bin()
function.
Example:
# Hexadecimal string
hex_value = "1A3F"
# Convert hexadecimal to integer
decimal_value = int(hex_value, 16)
# Convert integer to binary (removes '0b' prefix)
binary_value = bin(decimal_value)[2:]
print(f"Hexadecimal: {hex_value}")
print(f"Binary: {binary_value}")
Output:
Hexadecimal: 1A3F
Binary: 1101000111111
Explanation:
int(hex_value, 16)
converts the hexadecimal string1A3F
to its decimal integer equivalent (6719
).bin(decimal_value)[2:]
converts the integer6719
to binary (0b1101000111111
), and the[2:]
slices off the0b
prefix that Python uses to indicate binary numbers.
Method 2: Using a Custom Function (Manual Conversion)
If you want to implement the conversion manually, you can iterate through the hexadecimal string and convert each hexadecimal digit to its binary equivalent.
Example:
def hex_to_bin(hex_value):
# Create a dictionary for hexadecimal to binary mapping
hex_to_bin_map = {
'0': '0000', '1': '0001', '2': '0010', '3': '0011',
'4': '0100', '5': '0101', '6': '0110', '7': '0111',
'8': '1000', '9': '1001', 'A': '1010', 'B': '1011',
'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'
}
# Convert each hex digit to its binary equivalent
binary_value = ''.join(hex_to_bin_map[digit] for digit in hex_value.upper())
return binary_value
# Example usage
hex_value = "1A3F"
binary_value = hex_to_bin(hex_value)
print(f"Hexadecimal: {hex_value}")
print(f"Binary: {binary_value}")
Output:
Hexadecimal: 1A3F
Binary: 0001101000111111
Explanation:
- The
hex_to_bin_map
dictionary maps each hexadecimal digit to its corresponding 4-bit binary representation. - The
join()
method is used to concatenate the binary strings for each hexadecimal digit in the string.
Method 3: Using format()
Function
You can also use the format()
function in Python to convert the integer to binary.
Example:
# Hexadecimal string
hex_value = "1A3F"
# Convert hexadecimal to integer
decimal_value = int(hex_value, 16)
# Convert integer to binary using format function
binary_value = format(decimal_value, 'b')
print(f"Hexadecimal: {hex_value}")
print(f"Binary: {binary_value}")
Output:
Hexadecimal: 1A3F
Binary: 1101000111111
Explanation:
int(hex_value, 16)
converts the hexadecimal string to an integer.format(decimal_value, 'b')
converts the integer to a binary string without the0b
prefix.
Summary:
- Method 1: Convert using
int()
to get the decimal equivalent, then usebin()
to convert to binary. - Method 2: Manually map each hex digit to binary using a dictionary.
- Method 3: Use
format()
to convert the integer to binary.
All three methods are valid and depend on the preference for built-in functions versus manual mapping.