Skip to main content

Binary and hexadecimal conversion explained

6 min readUpdated

Number conversion is in almost every computer science exam. Computers store everything in binary, and hexadecimal is a short way for humans to write binary. With place values you can convert any way in a minute.

Place value is the key idea

In denary (base 10) each column is worth 10 times the one to its right. In binary (base 2) each column is worth 2 times, and in hexadecimal (base 16) each column is worth 16 times.

10110100 in binary = 128 + 32 + 16 + 4 = 180
1286432168421
10110100

Denary to binary

  1. Write the column values 128, 64, 32, 16, 8, 4, 2, 1.
  2. Start at the left: if the column fits into what is left, write 1 and subtract it; otherwise write 0.
  3. Carry on until you reach the 1 column.

Example: 77. 64 fits (13 left), 32 no, 16 no, 8 fits (5 left), 4 fits (1 left), 2 no, 1 fits. Answer: 01001101.

Hexadecimal digits

DenaryBinaryHex
910019
101010A
111011B
121100C
131101D
141110E
151111F

Binary to hex and back

One hex digit is exactly four bits (a nibble). Split the binary into groups of four from the right, then convert each group.

Nibble methodtext
1011 0100
  B    4      ->  B4

3F  ->  0011 1111

Hex to denary

Multiply the left digit by 16 and add the right digit. B4 = 11 × 16 + 4 = 180, the same as the binary example above.

Check your answers with Python

python
print(bin(77))             # 0b1001101
print(hex(180))            # 0xb4
print(int("10110100", 2))  # 180
print(int("B4", 16))       # 180

Common mistakes

  • Grouping bits from the left instead of the right.
  • Forgetting that A to F stand for 10 to 15.
  • Dropping leading zeros when the question asks for 8 bits.
  • Mixing up which column is 1 (it is always the rightmost).

Frequently asked questions

Why do programmers use hexadecimal?

It is much shorter than binary and converts to it easily, so it is used for colour codes, memory addresses and error codes.

What is the largest number in 8 bits?

11111111, which is 255 in denary and FF in hex.

Do I need to learn the hex table by heart?

Learning 10 to 15 as A to F is enough; everything else comes from place value.

Practise this, step by step

Short lessons with quizzes, practice and interview questions, from your first program to job-ready.

Python Programming course: syllabus and 1:1 mentoringCore Python from zero to functions, collections, files, OOP and algorithms.