Number Base Conversion Guide: Binary, Hex, Octal and More

Why different number bases exist and how to convert between them — with real examples you'll use as a developer.

Developer ToolsApril 11, 20268 min read

Why Number Bases Matter

Decimal (base 10) is what humans use. Binary (base 2) is what computers use. Hexadecimal (base 16) is the compromise — compact enough for humans to read, aligned closely enough to binary for machines. Octal (base 8) is a historical artifact that still shows up in Unix file permissions.

Understanding base conversion isn't academic trivia. You'll encounter it when reading CSS colors, debugging network packets, setting file permissions, working with bitwise flags, or reverse-engineering binary formats.

The Four Bases You'll Use

BaseNameDigitsPrefix/SuffixCommon Use
2Binary0–10bMachine code, bitmasks
8Octal0–70o / leading 0Unix permissions
10Decimal0–9noneHuman-readable numbers
16Hexadecimal0–9, A–F0x / #CSS colors, memory addresses

How Base Conversion Works

The fundamental principle: each position in a number represents a power of the base. In decimal, 255 means 2×10² + 5×10¹ + 5×10⁰. In binary, 11111111 means 1×2⁷ + 1×2⁶ + ... + 1×2⁰ = 255.

Decimal to Binary

Divide by 2 repeatedly, recording remainders from bottom to top:

42 ÷ 2 = 21 remainder 0
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5  remainder 0
 5 ÷ 2 = 2  remainder 1
 2 ÷ 2 = 1  remainder 0
 1 ÷ 2 = 0  remainder 1

42 in decimal = 101010 in binary

Binary to Hexadecimal

Group binary digits in sets of 4 (starting from the right), then convert each group:

Binary:  1101  0111  0010  1010
         D     7     2     A
Hex:     0xD72A

This is why hex is so popular — each hex digit maps cleanly to exactly 4 bits. One byte (8 bits) = exactly two hex digits. A 32-bit integer = 8 hex digits. This alignment is why memory addresses, color codes, and network protocols all use hex.

Practical Examples

CSS Colors Are Hex

When you write #8b5cf6, you're writing three base-16 numbers: red=8B, green=5C, blue=F6.

#8b5cf6 = rgb(139, 92, 246)

Red:   8B hex = 8×16 + 11 = 139
Green: 5C hex = 5×16 + 12 = 92
Blue:  F6 hex = 15×16 + 6 = 246

Understanding this conversion helps you work with color manipulation libraries, generate dynamic color schemes, or debug why your design system's purple looks different in dark mode.

Unix File Permissions

Octal notation is how you set file permissions on Linux. Each digit represents read (4), write (2), and execute (1) permissions:

chmod 755 script.sh
# Owner: 7 = 4+2+1 = read + write + execute
# Group: 5 = 4+0+1 = read + execute
# Other: 5 = 4+0+1 = read + execute

chmod 644 config.json
# Owner: 6 = 4+2+0 = read + write
# Group: 4 = 4+0+0 = read
# Other: 4 = 4+0+0 = read

IP Addresses and Subnet Masks

IPv4 addresses are written in dotted decimal, but they're really 32-bit binary numbers. Subnet masks are where base conversion becomes essential:

IP:   192.168.1.0
Binary: 11000000.10101000.00000001.00000000

Subnet: 255.255.255.0 (/24)
Binary: 11111111.11111111.11111111.00000000

The CIDR notation /24 means 24 bits are set to 1 in the mask. Converting between binary, decimal, and CIDR is daily work for network engineers.

Bitwise Operations

Bitwise operators work directly on binary representations. Common in systems programming, game development, and protocol implementation:

# Define flags
READ    = 0b001  # 1
WRITE   = 0b010  # 2
EXECUTE = 0b100  # 4

# Combine flags
permissions = READ | WRITE   # 0b011 = 3

# Check a flag
has_read = permissions & READ  # 0b011 & 0b001 = 0b001 (truthy)

# Remove a flag
read_only = permissions & ~WRITE  # 0b011 & 0b101 = 0b001 = 1

Converting in Programming Languages

# Python
int('FF', 16)           # 255 — hex string to int
hex(255)                # '0xff' — int to hex string
bin(255)                # '0b11111111' — int to binary string
format(255, '08b')      # '11111111' — padded binary
oct(0o755)              # '0o755' — octal literal to string

# JavaScript
parseInt('FF', 16)      // 255
(255).toString(16)      // 'ff'
(255).toString(2)       // '11111111'
0xFF === 255            // true — hex literal
0o755 === 493           // true — octal literal

Quick Conversion Table

DecimalBinaryOctalHex
0000000
7011177
81000108
10101012A
15111117F
16100002010
25511111111377FF

Use a Converter When Speed Matters

For quick conversions without opening a terminal or writing code, [RiseTop's number base converter](/tools/number-base-converter.html) handles binary, octal, decimal, and hexadecimal — converting between any pair instantly. It also supports custom bases (base 2 through 36) for specialized use cases like base32 encoding or base36 URL shorteners.

Key Takeaways