Converting Numbers Between Bases: A Developer's Reference
Number base conversion is a fundamental skill for systems programming, networking, graphics, and cryptography. This reference covers all the conversions you'll encounter regularly.
Convert numbers instantly: Our Number Base Converter converts between decimal, hex, binary, and octal — with live updates as you type.
Open Number Base Converter →Table of Contents
Quick Reference Table
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 4 | 0100 | 4 | 4 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 0001 0000 | 20 | 10 |
| 255 | 1111 1111 | 377 | FF |
| 256 | 0001 0000 0000 | 400 | 100 |
Converting to Decimal
Multiply each digit by its positional power and sum:
Binary 1011 = (1×8) + (0×4) + (1×2) + (1×1) = 11
Hex 2F = (2×16) + (15×1) = 47
Octal 755 = (7×64) + (5×8) + (5×1) = 509
Converting Decimal to Binary
Repeatedly divide by 2 and collect remainders bottom-to-top:
45 ÷ 2 = 22 remainder 1 ← LSB
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 ← MSB
Read remainders bottom-to-top: 101101 = 45
Hex ↔ Binary Shortcut
Each hex digit maps exactly to 4 bits — the fastest way to convert:
0=0000 1=0001 2=0010 3=0011
4=0100 5=0101 6=0110 7=0111
8=1000 9=1001 A=1010 B=1011
C=1100 D=1101 E=1110 F=1111
// 0xA5 → 1010 0101
// 0x3F → 0011 1111
// Binary 1101 0010 → 0xD2
In Code
// JavaScript
parseInt("1011", 2) // binary → decimal: 11
parseInt("FF", 16) // hex → decimal: 255
(255).toString(2) // decimal → binary: "11111111"
(255).toString(16) // decimal → hex: "ff"
# Python
int("1011", 2) # 11
int("FF", 16) # 255
bin(255) # "0b11111111"
hex(255) # "0xff"
oct(255) # "0o377"
// Go
strconv.ParseInt("FF", 16, 64)
strconv.FormatInt(255, 2)
When You'll Use Each Base
- Binary: Bit flags, masks, low-level hardware, network protocols
- Octal: Unix permissions (
chmod 755), some legacy file formats - Decimal: User-facing numbers, most application logic
- Hex: Memory addresses, colors (
#FF5733), MAC addresses, hash values, byte dumps
Common Conversion Mistakes
The most frequent error when converting between number bases is confusing the direction of the conversion. When converting decimal to binary by repeated division, the remainders must be read from bottom to top — reading them in the order they're computed gives you the number reversed. A related mistake is forgetting that leading zeros matter in certain contexts: the byte 0x0A is 10 in decimal, and dropping the leading zero changes nothing mathematically, but tools that expect fixed-width representations (like hex editors or network analyzers) will misinterpret the value.
Another common pitfall is mixing up signed and unsigned representations. In two's complement, the binary value 11111111 is 255 as an unsigned byte but -1 as a signed byte. If you're debugging memory dumps or network packets, getting this wrong can send you chasing the wrong value entirely.
Octal is a frequent source of bugs in JavaScript specifically. A numeric literal starting with 0 (like 0755) is interpreted as octal in non-strict mode, which means 0100 is 64, not 100. This is why modern JavaScript uses the explicit 0o prefix for octal (0o755) and strict mode disallows the old syntax.
Real-World Debugging Scenarios
Number base conversion comes up constantly in practical work. When reading a hex dump from a network packet capture, each pair of hex digits represents a single byte. If you're analyzing a TCP header and see 01BB in the destination port field, converting from hex to decimal gives you 443 — the standard HTTPS port. Without being comfortable with hex-to-decimal conversion, network debugging is nearly impossible.
Color values in CSS and design tools use hex extensively. The color #FF6B35 breaks down to R=255, G=107, B=53. Understanding this mapping lets you tweak colors numerically rather than relying on a picker — adding 20 to the green channel shifts the hue toward yellow, and you can calculate that directly.
Unix file permissions are the classic octal use case. The permission 755 breaks down to three octal digits: owner (7 = rwx = 111 in binary), group (5 = r-x = 101), and others (5 = r-x = 101). Each digit maps to a 3-bit field controlling read, write, and execute access. Understanding this octal-to-binary relationship makes permission debugging instantaneous.
Memory addresses in debuggers and crash logs are always hexadecimal. A segmentation fault at address 0x7FFEE3B0C4A0 tells you the memory region (the high bytes indicate whether it's stack, heap, or mapped memory) — but only if you can read hex fluently.
Base Conversion in Different Languages
Most modern languages have built-in base conversion, but the APIs differ. In Python, int() accepts a base parameter for parsing, while bin(), hex(), and oct() format numbers with prefixes. JavaScript's parseInt() and Number.toString() handle arbitrary bases from 2 to 36. In C and C++, strtol() converts strings with a base parameter, while format specifiers like %x, %o, and %b (C23) handle output.
For languages that lack native base conversion — or when you need bases beyond 36 — the algorithm is always the same: repeatedly divide by the target base and collect remainders. This is worth knowing even if you never implement it yourself, because it demystifies what the built-in functions are doing under the hood.
