Number Base Conversion: Binary, Hex, Octal, and Decimal
Every number you see on screen is stored as binary — ones and zeros — inside the processor. Understanding how to convert between binary, octal, decimal, and hexadecimal is a foundational skill for anyone working with low-level programming, networking, or digital electronics.
The Four Number Bases Programmers Use Daily
Binary (base 2) uses only 0 and 1. It maps directly to the on/off states of transistors and is the native language of CPUs. Every instruction your processor executes is ultimately binary.
Octal (base 8) groups binary digits into sets of three. It appears most often in Unix file permissions: chmod 755 sets read-write-execute for the owner (7 = 111 in binary), read-execute for the group (5 = 101), and read-execute for others.
Decimal (base 10) is what humans default to — likely because we have ten fingers. It is the standard for user-facing values like prices, counts, and measurements.
Hexadecimal (base 16) uses digits 0-9 and letters A-F. One hex digit represents exactly four binary bits (a nibble), making it a compact way to express binary data. Memory addresses, color codes (#FF5733), and MAC addresses all use hex.
How Binary to Decimal Conversion Works
To convert binary to decimal, multiply each bit by its positional power of 2, then sum the results. For example, the binary number 1011 0010:
- Bit 7: 1 × 128 = 128
- Bit 6: 0 × 64 = 0
- Bit 5: 1 × 32 = 32
- Bit 4: 1 × 16 = 16
- Bit 3: 0 × 8 = 0
- Bit 2: 0 × 4 = 0
- Bit 1: 1 × 2 = 2
- Bit 0: 0 × 1 = 0
Total: 128 + 32 + 16 + 2 = 178. In hex, that is B2. In octal, 262.
Why Hex Dominates in Software
Hexadecimal is the preferred human-readable format for binary data because the conversion is mechanical: each hex digit maps to exactly 4 bits. A 32-bit memory address like 11000000 10101000 00000001 00000001 is unreadable, but its hex equivalent C0A80101 (which is IP address 192.168.1.1) is manageable.
CSS color codes use hex because three bytes (24 bits) neatly encode red, green, and blue channels as six hex digits. The shorthand #F00 expands to #FF0000 — pure red at full intensity.
Practical Conversion Shortcuts
Binary ↔ Hex: Group binary digits into sets of four from right to left, then map each group to its hex equivalent. 1010 1111 = AF. This works because 16 = 2⁴.
Binary ↔ Octal: Group into sets of three from right to left. 010 101 111 = 257 in octal. This works because 8 = 2³.
Decimal to any base: Repeatedly divide by the target base and collect remainders in reverse order. For 255 ÷ 16: quotient 15 remainder 15 → quotient 0 remainder 15 → hex FF.
Common Pitfalls in Base Conversion
- Leading zeros matter in context —
0x0Ais decimal 10, but in many languages a leading0signals octal:010is 8 in C/C++ and JavaScript (strict mode throws an error). Python 3 requires0o10for octal. - Case insensitivity —
0xFFand0xffare identical. Convention varies: CSS uses lowercase, memory dumps use uppercase, and most languages accept both. - Signed vs unsigned — The binary
1111 1111is 255 unsigned but -1 in signed 8-bit two's complement. Context determines interpretation. - BigInt for large values — JavaScript
Numberloses precision above 2⁵³ - 1 (9,007,199,254,740,991). UseBigIntfor 64-bit addresses and cryptographic hashes.
Key Takeaways
- Binary is the machine's native language; hex is the programmer's shorthand for it
- Group binary into 4-bit chunks for hex or 3-bit chunks for octal — no arithmetic needed
- Watch for language-specific octal literals (
0ovs leading0) to avoid silent bugs - Use BigInt when working with values larger than 2⁵³ to prevent precision loss
Ready to convert? Use our Binary to Decimal Converter to instantly translate between binary, octal, decimal, and hexadecimal — right in your browser, no sign-up required.
Frequently Asked Questions
- Why do programmers use hexadecimal instead of binary?
- Hexadecimal is a compact representation of binary. Each hex digit maps to exactly 4 binary bits, so a 32-bit value takes 8 hex characters instead of 32 binary digits. This makes memory addresses, color codes, and byte values much easier to read and communicate.
- How do you convert binary to hexadecimal without a calculator?
- Group the binary digits into sets of four from right to left, padding with leading zeros if needed. Then convert each group to its hex equivalent: 0000=0, 0001=1, ..., 1001=9, 1010=A, ..., 1111=F. For example, 1010 1111 becomes AF.
- What is the largest number a single byte can represent?
- A byte is 8 bits. As an unsigned value, the range is 0 to 255 (binary 0000 0000 to 1111 1111, hex 00 to FF). As a signed two's complement value, the range is -128 to 127.