Number base conversion: binary, decimal, hexadecimal
How to convert between bases, and why binary and hexadecimal are everywhere in computing.
Common bases
| Base | Name | Digits used | Example |
|---|---|---|---|
| 2 | Binary | 0, 1 | 1010 |
| 8 | Octal | 0 to 7 | 12 |
| 10 | Decimal | 0 to 9 | 10 |
| 16 | Hexadecimal | 0-9, A-F | A |
Binary to decimal
Each position is worth a power of 2, starting from the right (2⁰, 2¹, 2²…).
1010 (binary) = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10 (decimal)
Decimal to binary
Repeated division by 2, keeping the remainders, read bottom to top:
10 ÷ 2 = 5 remainder 0 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 → 1010
Hexadecimal
Each hex digit maps exactly to 4 bits (a "nibble"), which makes binary ↔ hex conversion direct, 4 bits at a time:
1010 1111 (binary) = A F (hexadecimal)
That direct mapping is why web colors are written in hex (#FF5733) and memory addresses are almost always shown in hex rather than binary.
In practice
# In a terminal (bash): echo $((2#1010)) # binary to decimal: 10 echo $((16#FF)) # hex to decimal: 255 printf '%x\n' 255 # decimal to hex: ff
Thanks for the feedback!