Binary to hex: four bits per digit
This binary to hex converter packs long runs of 0s and 1s into short hexadecimal strings. Since 16 = 2^4, the conversion needs no arithmetic at all: every group of four bits, called a nibble, maps directly to one hex digit.
The nibble table
| Bits | Hex | Bits | Hex |
|---|---|---|---|
| 0000 | 0 | 1000 | 8 |
| 0001 | 1 | 1001 | 9 |
| 0010 | 2 | 1010 | A |
| 0011 | 3 | 1011 | B |
| 0100 | 4 | 1100 | C |
| 0101 | 5 | 1101 | D |
| 0110 | 6 | 1110 | E |
| 0111 | 7 | 1111 | F |
Worked example
Start at the right-hand end and split the bits into groups of four, padding the leftmost group with zeros if needed.
110101111
→ 1 1010 1111
→ 0001 1010 1111
→ 1 A F = 1AF
For a fraction, group outward from the binary point: to the left as usual, and to the right padding the last group with trailing zeros. 101.101 becomes 0101 . 1010, which is 5.A.
Example output
Input Output
1010 A
11111111 FF
100000000 100
10110011 B3
101.101 5.A
0b10110 16
Options and input tips
- Letters: choose A–F or a–f. Lower case is common in hashes and CSS; upper case is common in datasheets.
- Grouped input: spaces and underscores are ignored. Pasting
1101 0110is therefore read as11010110and returnsD6, and it works the same when the groups are not exactly four bits long. The field holds one number at a time. - Prefixes and length: a leading
0bis accepted, and there is no length limit.
Common slips
- Grouping from the left. 110101111 grouped as 1101 0111 1 gives nonsense; always start from the right-hand end.
- Confusing B (1011) with D (1101), or 6 (0110) with 9 (1001). The table above is worth keeping nearby until the patterns become automatic.
When hex beats binary
A 32-bit value is 32 characters in binary but only 8 in hex, and a byte is always exactly two hex digits. That is why masks, register values, checksums and machine code are almost always written in hex, even though the hardware thinks in bits.
For the reverse direction, use hex to binary. If you would rather group bits in threes, binary to octal does that, and binary to decimal gives the ordinary numeric value.
Binary to hex in code and spreadsheets
- Python:
format(int('110101111', 2), 'X')returns'1AF', andhex()gives'0x1af'. Python integers have no size limit. - JavaScript:
parseInt('110101111', 2).toString(16)returns'1af', but it is only exact up to 53 bits. For longer strings useBigInt('0b' + bits).toString(16). - C and C++: binary literals such as
0b110101111are standard since C++14 and C23; print them with%X. - Excel and Google Sheets:
=BIN2HEX("11111111")returnsFF. The input may have at most 10 bits, so split longer values into nibbles or use this converter.
The converter is handy when those limits get in the way: it accepts strings of any length, keeps fractions and shows the octal and decimal forms of the same value next to the hex result.