Skip to main content

← Back to blog

Binary, Decimal and Hex Explained: How to Convert Between Number Bases

We count in decimal (base 10) because we have ten fingers. Computers count in binary (base 2) because a wire is either on or off. Programmers lean on hexadecimal (base 16) because it’s a compact, tidy way to write binary. Converting between these bases is a daily task once you work close to the metal — and it’s simpler than it looks.

What “base” actually means

A base is just how many symbols each digit can be, and how much each position is worth:

  • Decimal (10): digits 0–9. Positions are 1, 10, 100, 1000…
  • Binary (2): digits 0–1. Positions are 1, 2, 4, 8, 16…
  • Octal (8): digits 0–7. Positions are 1, 8, 64…
  • Hex (16): digits 0–9 then a–f (a=10 … f=15). Positions are 1, 16, 256…

So binary 1010 = 8 + 0 + 2 + 0 = 10 in decimal. Hex ff = 15×16 + 15 = 255.

Why hex, not just binary?

Binary is correct but unreadable — 11111111 vs ff. Because 16 is 2⁴, every hex digit maps to exactly four binary digits, so hex is a shorthand for binary with no loss. That’s why colors (#3388cc), memory addresses (0x7ffe) and Unicode code points (U+1F600) are written in hex.

The one gotcha: the same digits mean different numbers

10 is ten in decimal, two in binary, eight in octal and sixteen in hex. A string of digits is meaningless until you know its base. That’s the mistake behind most conversion errors — assuming 100 is a hundred when it’s actually binary for four.

Text is just bytes too

“Text to binary” works because every character is stored as bytes (via UTF-8), and every byte is 8 bits. The letter A is byte 65 = 01000001 in binary = 41 in hex. Emoji and non-Latin characters take several bytes, but the idea is the same.

Convert instantly, in your browser

The number base converter links all four bases: type into any field and the rest update live. It uses BigInt, so even huge numbers stay exact, and it also turns text into binary or hex (and back) using UTF-8.

A quick reference table

Every row below is the same number in four bases — handy to keep nearby:

DecimalBinaryHexOctal
0000
81000810
101010A12
151111F17
16100001020
64100000040100
100110010064144
1281000000080200
25511111111FF377
256100000000100400
1024100000000004002000

The row worth memorising is 255 = 0xFF = 0b11111111 — the largest value a single byte (8 bits) can hold.

Converting by hand

  • Decimal → binary: divide by 2 repeatedly and read the remainders bottom-up. 13 → 6 r1, 3 r0, 1 r1, 0 r11101.
  • Binary → decimal: add the place values. 1101 = 8 + 4 + 0 + 1 = 13.
  • Binary ↔ hex: group the bits into nibbles of four from the right. 111111111111 1111F FFF.
  • Hex → decimal: multiply each digit by its power of 16. 0x2A = 2×16 + 10 = 42.

What the prefixes mean

Because the same digits mean different numbers, code marks the base with a prefix: 0x for hex (0xFF), 0b for binary (0b1010), and 0o (or a bare leading zero in C) for octal (0o17). No prefix means decimal. When you’d rather not do any of this by hand, the guide to CSS color formats shows the same hex at work in web colors.