How to Convert Binary to Hex
Split the bits into groups of four, starting from the right, and swap each group for one hex digit. This guide walks through that method and the cases that trip people up. To check an answer as you go, use the binary to hex converter.
Why four bits make one hex digit
Binary has two digits, 0 and 1. Hexadecimal has sixteen: 0 to 9, then A to F for the values 10 to 15. Four bits can form 2 × 2 × 2 × 2 = 16 patterns, so each pattern matches exactly one hex digit. That means you can convert one group at a time, without working out the value of the whole number.
The sixteen patterns
Each bit in a group is worth 8, 4, 2 or 1. Add the values under the 1s to get the digit.
| Binary | Hex | Decimal |
|---|---|---|
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | A | 10 |
| 1011 | B | 11 |
| 1100 | C | 12 |
| 1101 | D | 13 |
| 1110 | E | 14 |
| 1111 | F | 15 |
Convert binary to hex in four steps
The example is 1011110101.
Group in fours from the right
You get 10, 1111 and 0101.
Pad the left group
The group 10 is two bits short. Add two zeros in front to make 0010.
Swap each group for a digit
0010 is 2. 1111 is 8 + 4 + 2 + 1 = 15, which is F. 0101 is 4 + 1 = 5.
Join the digits in order
Read them left to right. The answer is 2F5.
When a number could be read in more than one base, label it. Programmers often write 0x2F5, and maths courses write 2F5 with a small 16 below it.
Check your answer through decimal
A second method is a useful test. Convert the binary number to decimal, then divide by 16 and keep the remainders. Take 11010110. The places holding a 1 add up to 128 + 64 + 16 + 4 + 2 = 214.
214 ÷ 16 is 13 with remainder 6. Then 13 ÷ 16 is 0 with remainder 13, which is D. Read the remainders from last to first to get D6. Grouping gives the same result: 1101 is D and 0110 is 6.
This route takes longer, and the gap grows with the length of the number. You can practise each half with binary to decimal and decimal to hex.
Binary numbers with a point
Group outwards from the point. Left of it, group from right to left and pad on the left. Right of it, group from left to right and pad on the right.
For 1010.011, the left side is 1010, which is A. The right side is 011, which pads to 0110 and gives 6. The answer is A.6. Padding the right side on the wrong end would give 0011, or A.3, which is a different value.
Negative numbers and two's complement
Hex only rewrites the bits. It doesn't record whether they stand for a negative number. In 8-bit two's complement, −42 is stored as 11010110, so its hex form is D6. Read as an unsigned number, the same D6 is 214.
So check the bit width and whether the type is signed before you turn a hex value back into decimal. The binary converter shows two's complement forms.
Binary to hex for text
Text stored in binary converts the same way, and each 8-bit byte becomes two hex digits. The word "Hi" is01001000 01101001 in ASCII. In hex that's 48 69. Look up 48 and 69 in the ASCII table and you'll find H and i. To decode a longer message, use binary to text.
Keep leading zeros when you work with bytes. The byte 00001111 is written 0F, so every byte stays two digits wide. As a plain number, it can be written F.
Convert binary to hex in code
Each example reads a string of bits and prints it in capital hex.
Python
bits = '110101101011'
print(format(int(bits, 2), 'X')) # D6BJavaScript
const bits = '110101101011';
console.log(BigInt('0b' + bits).toString(16).toUpperCase()); // D6BC
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%lX\n", strtoul("110101101011", NULL, 2)); /* D6B */
return 0;
}Bash
printf '%X\n' "$((2#110101101011))" # D6BThese calls drop leading zeros. In Python, format(int(bits, 2), '02X') keeps a single byte two digits wide. The C and Bash examples are limited by the size of their integer types, so they aren't suited to very long strings.
Excel and Google Sheets
Use =BIN2HEX("11010110") to get D6. The function takes at most 10 bits, and a 10-bit input that starts with 1 is treated as negative. A second argument sets the width: =BIN2HEX("1111", 2) gives 0F.
Common mistakes
Grouping from the wrong side
For a whole number, start at the right. 1011010 splits into 101 and 1010, which is 5A. Grouping from the left gives 1011 and 010, and the wrong answer B2.
Dropping a group of zeros
1111 0000 1111 is F0F. The middle group still needs its digit.
Swapping B and D
B is 1011, or 8 + 2 + 1 = 11. D is 1101, or 8 + 4 + 1 = 13.
Practice questions
Work each one out before you open the answer.
Convert 100111 to hex
27. The groups are 0010 and 0111.
Convert 11111010 to hex
FA. The groups are 1111 and 1010.
Convert 1000000001 to hex
201. The groups are 0010, 0000 and 0001.
Convert 1011111011101111 to hex
BEEF. The groups are 1011, 1110, 1110 and 1111.