Every developer runs into number base conversions eventually — reading memory addresses in hex, working with file permissions in octal, or debugging bitwise operations in binary. Here's a practical guide to all four bases.
Why Different Number Bases?
We count in base 10 because we have ten fingers. Computers work in base 2 because a transistor has two states (on/off). Hex (base 16) is a compact representation of binary — each hex digit exactly represents 4 bits. Octal (base 8) represents 3 bits per digit, and was common in older Unix systems (file permissions are still in octal).
| Base | Name | Digits | 1 byte = |
|---|---|---|---|
| 2 | Binary | 0, 1 | 8 digits |
| 8 | Octal | 0–7 | 3 digits |
| 10 | Decimal | 0–9 | 3 digits |
| 16 | Hex | 0–9, A–F | 2 digits |
Binary (Base 2)
Each position represents a power of 2. The rightmost bit is 2⁰ = 1, then 2¹ = 2, 2² = 4, 2³ = 8, and so on.
Binary: 1 0 1 1 0 1
Powers: 32 16 8 4 2 1
Value: 32 0 8 4 0 1 = 45
Binary matters when you're doing bitwise operations:
// Set a bit
const flags = 0b0000;
const withFlag1 = flags | 0b0001; // 0b0001 = 1
const withFlag3 = flags | 0b0100; // 0b0100 = 4
// Check a bit
const hasFlag1 = (withFlag3 & 0b0001) !== 0; // false
// Toggle a bit
const toggled = withFlag3 ^ 0b0100; // 0b0000
Hexadecimal (Base 16)
Hex uses A–F for values 10–15. Each digit represents 4 bits (a "nibble"), so a byte (8 bits) is always two hex digits.
0x1F = 1×16 + 15 = 31
0xFF = 15×16 + 15 = 255 (max byte value)
0x2F855A = a color in CSS
You see hex everywhere:
- Colors:
#2f855a→ R=47, G=133, B=90 - Memory addresses:
0x7fff5fbff9c8 - SHA hashes:
a591a6d40bf420404a011733cfb7b190 - UUIDs:
550e8400-e29b-41d4-a716-446655440000
Octal (Base 8)
Octal uses digits 0–7. The main place you'll encounter it today: Unix file permissions.
chmod 755 script.sh # rwxr-xr-x
# 7 = 111 (binary) = rwx
# 5 = 101 (binary) = r-x
Each octal digit represents 3 bits: read (4), write (2), execute (1). So 7 = 4+2+1 = rwx, 6 = 4+2+0 = rw-, 5 = 4+0+1 = r-x.
Converting in JavaScript
// Decimal to other bases
(255).toString(2); // "11111111" (binary)
(255).toString(8); // "377" (octal)
(255).toString(16); // "ff" (hex)
// Other bases to decimal
parseInt("11111111", 2); // 255
parseInt("377", 8); // 255
parseInt("ff", 16); // 255
// Literals
const bin = 0b11111111; // 255
const oct = 0o377; // 255
const hex = 0xff; // 255
// For large numbers, use BigInt
BigInt("0x" + "ffffffffffffffff").toString(10);
Converting in Python
# Decimal to other bases
bin(255) # '0b11111111'
oct(255) # '0o377'
hex(255) # '0xff'
# Without prefix
format(255, 'b') # '11111111'
format(255, 'o') # '377'
format(255, 'x') # 'ff'
format(255, 'X') # 'FF' (uppercase)
# Other bases to decimal
int('11111111', 2) # 255
int('377', 8) # 255
int('ff', 16) # 255
Quick Conversion Tool
For quick one-off conversions without code — pasting a hex color, reading a binary value, checking an octal permission — SnappyTools' number base converter converts any value across all four bases simultaneously. Type in any field and the others update live.
Common Gotchas
Leading zeros in octal — In older JavaScript (pre-ES5), a number starting with 0 was treated as octal. 010 === 8 in old JS. Use 0o prefix in modern JS to be explicit.
Case sensitivity in hex — 0xFF and 0xff are the same. Uppercase is common for memory addresses; lowercase is conventional for CSS colors.
Signed vs unsigned — parseInt("ff", 16) returns 255 (unsigned). If you're working with signed bytes, 0xFF represents -1 in two's complement.
Summary
- Binary: closest to hardware, essential for bitwise ops
- Hex: compact binary representation, ubiquitous in web/systems work
- Octal: mainly Unix file permissions today
- Decimal: human-friendly, use for display
Use snappytools.app/number-base-converter/ for instant multi-base conversion without writing code.
Top comments (0)