🍋
Menu
How-To Beginner 2 min read 331 words

Number Base Conversion: Binary, Octal, Decimal, and Hexadecimal

Understanding number bases is essential for programming, networking, and digital electronics. This guide explains conversion methods between binary, octal, decimal, and hexadecimal systems.

Key Takeaways

  • Repeatedly divide by the target base and collect remainders in reverse order.
  • Multiply each digit by the base raised to its position power (counting from 0 at the right).
  • Each hex digit maps to exactly 4 binary digits:
  • Binary**: CPU operations, bitwise flags

The Four Common Bases

Base Name Digits Prefix
2 Binary 0, 1 0b
8 Octal 0-7 0o
10 Decimal 0-9 (none)
16 Hexadecimal 0-9, A-F 0x

Decimal to Any Base

Repeatedly divide by the target base and collect remainders in reverse order.

Example: Convert 156 to binary:

  • 156 / 2 = 78 remainder 0
  • 78 / 2 = 39 remainder 0
  • 39 / 2 = 19 remainder 1
  • 19 / 2 = 9 remainder 1
  • 9 / 2 = 4 remainder 1
  • 4 / 2 = 2 remainder 0
  • 2 / 2 = 1 remainder 0
  • 1 / 2 = 0 remainder 1

Read remainders bottom-up: 10011100

Any Base to Decimal

Multiply each digit by the base raised to its position power (counting from 0 at the right).

Example: 0xFF to decimal: 15 * 16^1 + 15 * 16^0 = 240 + 15 = 255

Binary-Hex Shortcut

Each hex digit maps to exactly 4 binary digits:

Hex Binary Hex Binary
0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111

To convert binary to hex: group bits in fours from the right, then replace each group. 100111001001 11009C.

Where Each Base Is Used

  • Binary: CPU operations, bitwise flags
  • Octal: Unix file permissions (chmod 755)
  • Hex: Colors (#FF6B35), memory addresses, MAC addresses, cryptographic hashes
  • Decimal: Human-facing values