How do you convert hex to binary?
Swap every hexadecimal digit for the four binary digits it represents, then write them side by side. Since 16 is 2 to the power 4, one hex digit always corresponds to exactly four bits, so the conversion is a lookup rather than a calculation. A5 becomes 1010 followed by 0101, giving 10100101.
People usually go this direction to see which individual bits are switched on: flags in a protocol header, a hardware register, a bitmask in a config file.
How to use it
- Enter the hex value, such as
CAFEorcafe. - Click Convert.
- Read the bits:
1100101011111110.
Expansion table
Each digit, its bits, and which of the four place values (8, 4, 2, 1) are set:
| Hex | Bits | Places set |
|---|---|---|
| 0 | 0000 | none |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 2+1 |
| 4 | 0100 | 4 |
| 5 | 0101 | 4+1 |
| 6 | 0110 | 4+2 |
| 7 | 0111 | 4+2+1 |
| 8 | 1000 | 8 |
| 9 | 1001 | 8+1 |
| a | 1010 | 8+2 |
| b | 1011 | 8+2+1 |
| c | 1100 | 8+4 |
| d | 1101 | 8+4+1 |
| e | 1110 | 8+4+2 |
| f | 1111 | 8+4+2+1 |
The padding gotcha
The converter treats your input as a number, and numbers don't keep leading zeros. That's correct mathematically but surprising when you're reading bytes:
| Input | Tool output | As a full byte |
|---|---|---|
7f |
1111111 |
01111111 |
0f |
1111 |
00001111 |
2C |
101100 |
00101100 |
F0 |
11110000 |
11110000 |
800 |
100000000000 |
0000100000000000 as 16 bits |
The rule for fixing it: a hex value of n digits should give 4n bits. If the output is shorter, add zeros on the left until it isn't. Skip this step and every bit position you count from the left will be off.
Worked example: decoding TCP flags
The TCP header has an 8-bit field of control flags. In RFC 9293 they run, from the high bit to the low bit: CWR, ECE, URG, ACK, PSH, RST, SYN, FIN. Packet tools often print this byte in hex. Suppose you see 0x12:
- Drop the prefix and convert
12. The tool returns10010. - Pad to 8 bits:
00010010. - Line the bits up with the flag names:
| CWR | ECE | URG | ACK | PSH | RST | SYN | FIN |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
ACK and SYN are set, so this is the SYN-ACK of a TCP handshake. Without padding, the five-digit output would have lined the 1s up under the wrong flags entirely.
The same method works for any bitmask: file mode bits, CPU status registers, or permission masks stored as hex in a database.
What it accepts and rejects
| Input | Result |
|---|---|
DEADBEEF |
32 bits: 11011110101011011011111011101111 |
ff |
11111111 (outer spaces trimmed) |
0x1f |
Error: remove 0x |
de ad |
Error: remove the space |
20000000000000 |
Error: too large (above 53 bits) |
| empty | Output cleared, no error |
The 53-bit cap exists because the conversion runs on JavaScript numbers, which are exact only up to Number.MAX_SAFE_INTEGER. Everything happens in your browser; nothing is uploaded.
Related tools
- Binary to hex compresses bits back into hex digits, four at a time.
- Hex to decimal gives the value as an ordinary number instead of bits.
- Hex to text is the right tool if your hex is a run of encoded characters rather than a single number.
- The random hex generator is handy for producing practice values.
Last updated