Convert hexadecimal numbers to binary
How to convert hex to binary in Python and by hand, what the conversion changes, how to keep leading zeros, and when the binary output stays usable after copying.
Converting hexadecimal to binary is a change of notation, not a change of value. Each hex digit stands for exactly four binary digits, so the conversion is lossless as long as you keep track of width and leading zeros. This review covers what the conversion does, how to do it reliably in Python, and when the binary form is worth using instead of hex or plain decimal.
Is hex-to-binary conversion suitable for your use?
Binary output is the right choice when you need to see individual bits: reading flags in a status register, checking a bitmask, explaining how a byte is built, or teaching number systems. Its main benefit is that every bit is visible at a glance. The decisive tradeoff is length. Binary is four times longer than hex, so long values become hard to scan, compare and transcribe accurately.
Keep the value in hex, or in decimal, when people need to read, quote or type it. Hex is the usual compact form for memory addresses, colour codes, hashes and byte dumps. Decimal is preferable when the number represents a quantity rather than a bit pattern. A practical rule is to convert to binary only for the part of the work that depends on individual bits, and to leave the stored or shared value in its original form.
What the conversion actually changes
Hexadecimal is base 16 and binary is base 2. Because 16 is 2 to the fourth power, each hex digit maps to one fixed group of four bits, often called a nibble. Converting means replacing each hex digit with its four-bit group. The underlying number is unchanged; only its written characters change.
| Hex | Binary | Decimal |
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| B | 1011 | 11 |
| C | 1100 | 12 |
| D | 1101 | 13 |
| E | 1110 | 14 |
| F | 1111 | 15 |
The single digit E, written as four bits, is 1110, which is 14 in decimal. Hex digits are case-insensitive, so e and E mean the same thing.
Worked examples
- 1E: 1 becomes 0001 and E becomes 1110, giving 0001 1110. Without the leading zeros that is 11110, which equals 30 in decimal.
- C1: C becomes 1100 and 1 becomes 0001, giving 1100 0001, which equals 193 in decimal.
- FF: each F becomes 1111, giving 1111 1111, which equals 255.
Two terms cause confusion. First, a hex string can be a number or a sequence of encoded bytes, such as text that was hex-encoded. Converting that string to binary gives you the bits of those bytes; it does not decode them back into readable text, which is a separate step. Second, the conversion does not reverse or reorder anything. If a protocol needs a different byte order (endianness) or bit order, that is an additional transformation you have to apply deliberately.
Converting hex to binary in Python
Python handles this in two steps: parse the hex string into an integer with int(text, 16), then format that integer in base 2. Python integers have arbitrary precision, so long hex values do not overflow.
>>> int("1e", 16)
30
>>> bin(int("1e", 16))
'0b11110'
>>> format(int("c1", 16), "b")
'11000001'
>>> f"{0x1e:08b}"
'00011110'
bin() adds a 0b prefix, while format() with the b specifier returns digits only. Both drop leading zeros, which is the most common source of errors. For example, int("0f", 16) formatted as binary gives 1111, losing the first nibble. To keep one four-bit group per hex digit, pad to four times the number of hex digits:
def hex_to_bin(hex_text):
digits = hex_text.strip().lower().removeprefix("0x")
return format(int(digits, 16), f"0{len(digits) * 4}b")
>>> hex_to_bin("0x1E")
'00011110'
>>> hex_to_bin("0f")
'00001111'
str.removeprefix requires Python 3.9 or later. Invalid characters raise a ValueError from int(), which is useful because it stops malformed input instead of producing a wrong result silently.
When the hex represents a byte sequence rather than a single number, convert it byte by byte so every byte keeps its eight bits:
>>> data = bytes.fromhex("c1 1e")
>>> "".join(f"{b:08b}" for b in data)
'1100000100011110'
For readability, the underscore option in the format specification groups binary digits in fours, so f"{0xc1:_b}" produces 1100_0001. Negative numbers are a limitation to note: bin(-30) returns -0b11110, a sign followed by the magnitude, not a two's complement bit pattern. If you need a fixed-width two's complement form, mask the value first, for example (-30) & 0xFF, before formatting.
Will the result stay readable and usable after copying?
Binary output consists only of the ASCII characters 0 and 1, plus any prefix or separators you choose, so it does not depend on special fonts or Unicode support. The practical risks come from formatting choices and from how the destination interprets a string of digits.
- Leading zeros: an application that treats the pasted value as a number rather than text may strip leading zeros. If width matters, store the value as text or keep the hex original alongside it.
- Separators: spaces and underscores improve readability but can break parsers. In Python, int("1100_0001", 2) is accepted, while int("1100 0001", 2) raises a ValueError. Remove separators before passing a value to code that expects continuous digits.
- Prefixes: 0b and 0x make the base explicit, which helps humans and Python, but some tools expect bare digits. Decide on one convention per document or data field.
- Search and comparison: the same value written as hex, binary with padding and binary without padding will not match in a plain text search. Searching works best on the original stored form.
Accessibility has not been verified here. No documentation or screen reader test was supplied, so this review cannot say how specific assistive technologies announce long binary strings. Long runs of identical digits are plausibly tedious to hear and easy to miscount, which is a reason to provide the hex or decimal equivalent next to any binary value that a reader must understand, and to group bits in fours where the destination allows it.
Online hex-to-binary encoders perform the same arithmetic, but the material supplied for this review gives no basis for judging how any particular tool handles or retains what you paste. For values that are sensitive, such as keys or internal identifiers, converting locally in Python avoids the question entirely.
Recommendation
Use binary when the task is about individual bits, and convert in Python with explicit padding so each hex digit keeps its four bits. Keep hex or decimal as the shared, stored and searchable form, and include it next to binary whenever people rather than programs will read the value.