Number Base Conversion Guide: Binary, Hex, Octal Explained
If you have ever stared at a hex color code like #1A2B3C or a Unix permission like chmod 755 and wondered where those numbers come from, you are not alone. Number bases are one of those foundational concepts that most developers use every day without thinking twice β until the moment they actually need to convert between them.
Let's fix that. This guide walks through the number systems that matter in programming, how to convert between them, and when each one shows up in practice.
What Does "Base" Actually Mean?
We count in base-10 (decimal) because we have ten fingers. Each digit position represents a power of 10:
4 2 7
| | |
| | βββ 7 Γ 10β° = 7
| βββββββ 2 Γ 10ΒΉ = 20
βββββββββββ 4 Γ 10Β² = 400
βββ
Total = 427
That is positional notation. The "base" (also called radix) tells you how many unique digits exist in the system and what each position multiplies by. Base-10 has digits 0β9. Base-2 has digits 0β1. Base-16 has digits 0β9 and AβF.
The concept is identical across all bases β only the number of available symbols and the multiplier per position changes.
Fun fact: the Babylonians used base-60 (sexagesimal). That is why we have 60 seconds in a minute and 360 degrees in a circle.
Binary (Base-2): The Language of Hardware
Why Computers Use Binary
Computers run on transistors, and transistors have two reliable states: on and off. That maps perfectly to 1 and 0. You could theoretically build a base-3 computer, but distinguishing between three voltage levels is harder and more error-prone than distinguishing between two. Binary wins because it is simple and robust.
Reading Binary Numbers
Binary works the same as decimal, but each position represents a power of 2:
1 0 1 1 0 1
| | | | | |
| | | | | βββ 1 Γ 2β° = 1
| | | | βββββββ 0 Γ 2ΒΉ = 0
| | | βββββββββββ 1 Γ 2Β² = 4
| | βββββββββββββββ 1 Γ 2Β³ = 8
| βββββββββββββββββββ 0 Γ 2β΄ = 0
βββββββββββββββββββββββ 1 Γ 2β΅ = 32
ββ
Total = 45
So 101101 in binary equals 45 in decimal. You read it right to left, doubling the place value each time: 1, 2, 4, 8, 16, 32, 64, 128...
Bits, Bytes, and Beyond
- Bit: a single binary digit (0 or 1)
- Nibble: 4 bits (represents one hex digit, values 0β15)
- Byte: 8 bits (values 0β255)
- Kilobyte: 1,024 bytes (2ΒΉβ°)
A single byte can represent 256 different values, which is why ASCII characters fit in one byte and RGB color channels go from 0 to 255.
Binary Arithmetic Basics
Addition in binary follows the same rules as decimal, but you carry at 2 instead of 10:
1 0 1 1 (11 in decimal)
+ 0 1 1 0 ( 6 in decimal)
βββββββββ
1 0 0 0 1 (17 in decimal)
When 1 + 1 = 10 in binary (just like 9 + 1 = 10 in decimal), you write 0 and carry 1.
Octal (Base-8): The Legacy System
A Brief History
Octal was popular in the 1960s and 70s with machines like the PDP-8, which used 12-bit words β neatly divisible into four groups of three bits. Each octal digit maps to exactly three binary digits, making it a convenient shorthand.
Where You Still See Octal
The most common place today is Unix file permissions. When you type chmod 755 script.sh, each digit is an octal number representing read (4), write (2), and execute (1) permissions:
7 = 4 + 2 + 1 = rwx (owner: read, write, execute)
5 = 4 + 0 + 1 = r-x (group: read, execute)
5 = 4 + 0 + 1 = r-x (others: read, execute)
Octal digits in programming use the 0o prefix (or just a leading 0 in C, which has caused plenty of bugs):
const permissions = 0o755; // 493 in decimal
console.log(permissions.toString(8)); // "755"
Watch out in JavaScript and Python: a leading zero like 0755 might be interpreted as octal in some contexts. Always use the explicit 0o prefix to avoid ambiguity.
Hexadecimal (Base-16): The Developer's Favorite
Why Hex Exists
Hexadecimal solves a practical problem: binary numbers get long fast. The decimal number 255 is 11111111 in binary β eight digits. In hex, it is just FF. Each hex digit maps to exactly four binary digits (a nibble), making conversions trivial and keeping numbers compact.
Hex uses digits 0β9 plus letters AβF:
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 15 | 1111 | F |
Hex in the Wild
Web colors are hex's most visible use. #FF5733 breaks down as:
#FF5733
ββββββ
ββββββββ Blue: 0x33 = 51
ββββββββ Green: 0x57 = 87
ββββββββ Red: 0xFF = 255
Memory addresses in debuggers display as hex: 0x7FFF5FBFFA10 is more readable than its decimal equivalent of 140,734,799,804,944.
MAC addresses use hex pairs separated by colons: A4:83:E7:2B:00:1F.
Color in CSS: rgba(255, 87, 51, 1.0) and #FF5733 represent the same color β hex is just more compact.
Converting Between Bases
Decimal to Binary
Repeatedly divide by 2 and collect the remainders, reading them bottom to top:
45 Γ· 2 = 22 remainder 1 β
22 Γ· 2 = 11 remainder 0 β
11 Γ· 2 = 5 remainder 1 β Read upward:
5 Γ· 2 = 2 remainder 1 β 101101
2 Γ· 2 = 1 remainder 0 β
1 Γ· 2 = 0 remainder 1 β
Result: 45 in decimal = 101101 in binary.
Decimal to Hexadecimal
Same approach, but divide by 16:
427 Γ· 16 = 26 remainder 11 (B) β
26 Γ· 16 = 1 remainder 10 (A) β Read upward: 1AB
1 Γ· 16 = 0 remainder 1 β
Result: 427 in decimal = 1AB in hex.
Binary to Hex (The Easy One)
Group binary digits into sets of four (from right to left), and convert each group:
Binary: 10 1101
Padded: 0010 1101
Groups: 2 D
Result: 0x2D
Verify: 0x2D = 2Γ16 + 13 = 45 β
This is why hex and binary are natural partners β the conversion is mechanical.
Hex to Binary
Reverse the process. Expand each hex digit to four binary digits:
Hex: F A 3
Binary: 1111 1010 0011
Result: 111110100011
Tip: memorize the binary patterns for 0βF (there are only 16 of them). Once you know those, hexβbinary conversions happen instantly in your head. Start with the powers of two: 1=0001, 2=0010, 4=0100, 8=1000.
Number Base Notation in Programming Languages
Every major language has syntax for writing numbers in different bases:
// JavaScript
const binary = 0b101101; // 45
const octal = 0o55; // 45
const hex = 0x2D; // 45
const decimal = 45; // 45
// Convert to string in any base
(45).toString(2); // "101101"
(45).toString(8); // "55"
(45).toString(16); // "2d"
// Parse from string
parseInt("101101", 2); // 45
parseInt("55", 8); // 45
parseInt("2D", 16); // 45
# Python
binary = 0b101101 # 45
octal = 0o55 # 45
hexval = 0x2D # 45
# Convert to string
bin(45) # '0b101101'
oct(45) # '0o55'
hex(45) # '0x2d'
# Parse from string
int("101101", 2) # 45
int("55", 8) # 45
int("2D", 16) # 45
// C / C++
int binary = 0b101101; // 45 (C23 / GCC extension)
int octal = 055; // 45 (leading zero = octal!)
int hex = 0x2D; // 45
Notice the C gotcha: a leading 0 means octal. Writing int x = 010; gives you 8, not 10. This has tripped up countless programmers.
Practical Use Cases
Bitwise Operations and Flags
Hex is the standard for bitmasks because each digit covers exactly four flags:
const READ = 0x01; // 0001
const WRITE = 0x02; // 0010
const EXECUTE = 0x04; // 0100
const ADMIN = 0x08; // 1000
let permissions = READ | WRITE; // 0x03 = 0011
if (permissions & EXECUTE) {
// check if execute bit is set
}
Debugging and Memory Inspection
When you see a segfault at address 0xDEADBEEF or initialize memory with 0xCAFEBABE, those are hex constants chosen specifically because they are easy to spot in memory dumps. Java class files start with the magic number 0xCAFEBABE β and yes, someone had fun naming that.
Network Protocols
IPv6 addresses use hex notation: 2001:0db8:85a3:0000:0000:8a2e:0370:7334. MAC addresses, USB vendor IDs, and Bluetooth UUIDs all use hex representation because it keeps binary data compact and readable.
Try It Yourself
Skip the manual math. Our Number Base Converter handles conversions between any bases instantly β decimal, binary, octal, hex, or any custom base up to 36. Paste a number, pick your source and target base, and get the result immediately. Everything runs in your browser, so nothing is sent to a server.
It is especially useful when you need to:
- Debug hex memory dumps by converting to decimal
- Calculate Unix permissions in binary/octal
- Verify color codes between hex and RGB values
- Convert between bases that are not powers of two
Related Resources
- Base64 Encoding Explained β Base64 uses a different approach (6-bit groups mapped to 64 characters) for encoding binary as text
- Hash Algorithms Compared β hash outputs are typically displayed in hexadecimal
- Number Base Converter β convert between any bases instantly in your browser
π οΈ Try it now: Number Base Converter β convert between binary, octal, decimal, hex, and any base up to 36. 100% client-side, completely free.