Number Base Conversion — Binary, Octal, Decimal, and Hexadecimal
Convert numbers between binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16). Includes step-by-step conversion methods, JavaScript parseInt/toString,...
Use the tool
Number Base Converter
Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.
Number base conversion is fundamental in programming — binary for bitwise operations, octal for Unix permissions, hex for colors and memory addresses. Here’s how to convert between them.
Use the Number Base Converter to convert between bases instantly.
Quick reference table
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 255 | 11111111 | 377 | FF |
| 256 | 100000000 | 400 | 100 |
Converting to decimal (any base)
Multiply each digit by its positional value (base^position):
Binary 1011₂ → Decimal:
1×2³ + 0×2² + 1×2¹ + 1×2⁰
= 8 + 0 + 2 + 1 = 11
Hex 1F₁₆ → Decimal:
1×16¹ + 15×16⁰ (F=15)
= 16 + 15 = 31
Octal 47₈ → Decimal:
4×8¹ + 7×8⁰
= 32 + 7 = 39
Converting decimal to other bases (repeated division)
Decimal 42 → Binary (divide by 2, collect remainders bottom-up):
42 ÷ 2 = 21 r 0
21 ÷ 2 = 10 r 1
10 ÷ 2 = 5 r 0
5 ÷ 2 = 2 r 1
2 ÷ 2 = 1 r 0
1 ÷ 2 = 0 r 1
Read remainders bottom-up: 101010₂
Decimal 255 → Hex (divide by 16):
255 ÷ 16 = 15 r 15 (F)
15 ÷ 16 = 0 r 15 (F)
Result: FF₁₆
JavaScript conversion
// parseInt(string, base) — any base to decimal:
parseInt('1011', 2) // 11 (binary to decimal)
parseInt('FF', 16) // 255 (hex to decimal)
parseInt('47', 8) // 39 (octal to decimal)
parseInt('42', 10) // 42 (decimal)
// Number.prototype.toString(base) — decimal to any base:
(255).toString(16) // 'ff' (decimal to hex)
(11).toString(2) // '1011' (decimal to binary)
(39).toString(8) // '47' (decimal to octal)
// JavaScript number literals:
const bin = 0b1011; // Binary literal = 11
const hex = 0xFF; // Hex literal = 255
const oct = 0o47; // Octal literal = 39
// Convert between bases:
function convert(value, fromBase, toBase) {
return parseInt(String(value), fromBase).toString(toBase);
}
convert('FF', 16, 2) // '11111111' (hex to binary)
convert('1011', 2, 16) // 'b' (binary to hex)
// Pad binary output:
(11).toString(2).padStart(8, '0') // '00001011'
Python conversion
# int(string, base) — any base to decimal:
int('1011', 2) # 11 (binary)
int('FF', 16) # 255 (hex)
int('47', 8) # 39 (octal)
# Built-in functions — decimal to binary/octal/hex:
bin(255) # '0b11111111'
oct(39) # '0o47'
hex(255) # '0xff'
# Clean output (without prefix):
format(255, 'b') # '11111111' (binary, no 0b)
format(255, 'o') # '377' (octal, no 0o)
format(255, 'x') # 'ff' (hex lowercase, no 0x)
format(255, 'X') # 'FF' (hex uppercase, no 0x)
# With padding:
format(11, '08b') # '00001011' (8 chars, zero-padded)
format(255, '04x') # '00ff'
# F-string shorthand:
n = 255
f'{n:b}' # '11111111'
f'{n:x}' # 'ff'
f'{n:08b}' # '11111111' (8 bits)
# Convert any base to any base:
def convert(value: str, from_base: int, to_base: int) -> str:
decimal = int(value, from_base)
if to_base == 10: return str(decimal)
digits = '0123456789ABCDEF'
result = ''
while decimal:
result = digits[decimal % to_base] + result
decimal //= to_base
return result or '0'
convert('FF', 16, 2) # '11111111'
convert('1011', 2, 16) # 'B'
Real-world uses
Binary (base 2):
- Bitwise operations
- Network subnet masks (255.255.255.0 = 11111111.11111111.11111111.00000000)
- Boolean flags (permissions, feature flags packed into one integer)
Octal (base 8):
- Unix file permissions: chmod 755 = rwxr-xr-x
7 = 111₂ = rwx (owner)
5 = 101₂ = r-x (group)
5 = 101₂ = r-x (other)
Hexadecimal (base 16):
- CSS colors: #FF5733 = R:255 G:87 B:51
- Memory addresses: 0x7ffee4f3
- Unicode code points: U+1F600 = 😀
- Byte display in hex editors
- SHA-256 hashes, API keys
Related tools
- Number Base Converter — convert between bases online
- Binary to Decimal Converter — binary conversion guide
- Hex Color Codes — hex in CSS colors
Related posts
- Binary Arithmetic — Addition, Subtraction, and Two's Complement — Learn how computers perform binary arithmetic: binary addition with carry, two's…
- Binary to Decimal — Convert Binary Numbers the Right Way — Binary to decimal conversion is foundational to understanding how computers stor…
- Binary to Text: How Binary Numbers Represent Characters — Binary to text conversion isn't magic — it's a lookup table. ASCII, Unicode, UTF…
- Hex Color Codes Explained — RGB, HSL, and How Colors Work in CSS — Hex color codes are 6-digit hexadecimal numbers that represent RGB values. Here'…
- Octal Converter — Convert Between Octal, Decimal, Binary, and Hex — Octal is base-8, using digits 0–7. It's used in Unix file permissions and legacy…
Related tool
Number Base Converter
Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.
Written by Mian Ali Khalid. Part of the Dev Productivity pillar.