Converting Between Number Systems
Table of Contents
Why Learn Number Conversion?
Number system conversion is a fundamental skill in computer science, networking, and digital electronics. While humans think in decimal, computers operate in binary, and many technical fields use hexadecimal or octal as convenient shorthand for binary data.
Here are real-world scenarios where you need to convert between number systems:
- Web development: CSS color codes use hexadecimal (e.g., #FF5733). To understand or manipulate colors programmatically, you need to convert between hex and decimal RGB values.
- Networking: IP addresses and subnet masks involve binary math. Calculating a subnet range requires converting dotted-decimal notation to binary, performing bitwise operations, and converting back.
- System administration: Unix file permissions (e.g., chmod 755) use octal. Each digit represents three binary bits controlling read, write, and execute permissions.
- Programming: Bitwise operations, bit flags, and low-level data manipulation all require understanding binary and hexadecimal representations.
- Hardware engineering: Memory addresses, register values, and bus data are all expressed in hex or binary.
The good news is that once you understand the underlying principles, conversion becomes mechanical. This guide walks you through every major conversion with step-by-step examples.
Decimal to Binary
To convert a decimal integer to binary, use the repeated division by 2 method. Divide the number by 2, record the remainder (0 or 1), then divide the quotient by 2 again. Continue until the quotient is 0. The binary result is the remainders read from bottom to top.
Example: Convert 156 to binary
| Step | Divide | Quotient | Remainder |
|---|---|---|---|
| 1 | 156 ÷ 2 | 78 | 0 |
| 2 | 78 ÷ 2 | 39 | 0 |
| 3 | 39 ÷ 2 | 19 | 1 |
| 4 | 19 ÷ 2 | 9 | 1 |
| 5 | 9 ÷ 2 | 4 | 1 |
| 6 | 4 ÷ 2 | 2 | 0 |
| 7 | 2 ÷ 2 | 1 | 0 |
| 8 | 1 ÷ 2 | 0 | 1 |
Reading remainders bottom to top: 156₁₀ = 10011100₂
Verification: 128 + 16 + 8 + 4 = 156 ✓
For decimal fractions: Multiply the fractional part by 2 repeatedly, recording the integer part each time.
Example: Convert 0.8125 to binary
| Step | Multiply | Integer Part | Remaining Fraction |
|---|---|---|---|
| 1 | 0.8125 × 2 | 1 | 0.625 |
| 2 | 0.625 × 2 | 1 | 0.25 |
| 3 | 0.25 × 2 | 0 | 0.5 |
| 4 | 0.5 × 2 | 1 | 0.0 |
Reading integer parts top to bottom: 0.8125₁₀ = 0.1101₂
Binary to Decimal
To convert a binary number to decimal, multiply each digit by its positional power of 2 and sum the results. This is also called the positional notation method.
Example: Convert 11010110₂ to decimal
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Binary Digit | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| Power of 2 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Value | 128 | 64 | 0 | 16 | 0 | 4 | 2 | 0 |
Sum: 128 + 64 + 16 + 4 + 2 = 214₁₀
A shortcut for common byte values is to memorize the powers of 2:
| Power | 2⁰ | 2¹ | 2² | 2³ | 2⁴ | 2⁵ | 2⁶ | 2⁷ | 2⁸ | 2⁹ | 2¹⁰ |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024 |
For binary fractions, use negative powers of 2. The first digit after the point is 2-1 (0.5), the second is 2-2 (0.25), and so on.
Example: Convert 0.1101₂ to decimal
(1 × 0.5) + (1 × 0.25) + (0 × 0.125) + (1 × 0.0625) = 0.5 + 0.25 + 0.0625 = 0.8125₁₀
Decimal to Hexadecimal
Converting decimal to hexadecimal uses the same repeated division method, but dividing by 16 instead of 2. Since hex uses digits 0–9 and letters A–F, remainders of 10–15 must be converted to their hex equivalents.
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
Example: Convert 4392 to hexadecimal
| Step | Divide | Quotient | Remainder | Hex Digit |
|---|---|---|---|---|
| 1 | 4392 ÷ 16 | 274 | 8 | 8 |
| 2 | 274 ÷ 16 | 17 | 2 | 2 |
| 3 | 17 ÷ 16 | 1 | 1 | 1 |
| 4 | 1 ÷ 16 | 0 | 1 | 1 |
Reading remainders bottom to top: 4392₁₀ = 1128₁₆
Verification: (1 × 4096) + (1 × 256) + (2 × 16) + (8 × 1) = 4096 + 256 + 32 + 8 = 4392 ✓
Another example with letters: Convert 255 to hexadecimal
| Step | Divide | Quotient | Remainder | Hex Digit |
|---|---|---|---|---|
| 1 | 255 ÷ 16 | 15 | 15 | F |
| 2 | 15 ÷ 16 | 0 | 15 | F |
Result: 255₁₀ = FF₁₆ — This is the maximum value of a single byte, which is why white in CSS is #FFFFFF.
Hexadecimal to Decimal
To convert hexadecimal to decimal, multiply each hex digit by its positional power of 16 and sum the results.
Example: Convert 2F1A₁₆ to decimal
| Position | Hex Digit | Decimal Value | Power of 16 | Result |
|---|---|---|---|---|
| 3 | 2 | 2 | 16³ = 4096 | 8192 |
| 2 | F | 15 | 16² = 256 | 3840 |
| 1 | 1 | 1 | 16¹ = 16 | 16 |
| 0 | A | 10 | 16⁰ = 1 | 10 |
Sum: 8192 + 3840 + 16 + 10 = 12,058₁₀
Common hex values you should know:
| Hex | Decimal | Binary | Common Use |
|---|---|---|---|
| 0x0A | 10 | 00001010 | Line feed (LF) |
| 0x0D | 13 | 00001101 | Carriage return (CR) |
| 0x20 | 32 | 00100000 | Space character |
| 0x30 | 48 | 00110000 | ASCII '0' |
| 0x41 | 65 | 01000001 | ASCII 'A' |
| 0x61 | 97 | 01100001 | ASCII 'a' |
| 0x7F | 127 | 01111111 | DEL character |
| 0xFF | 255 | 11111111 | Max byte value |
Binary to Hexadecimal
Converting between binary and hexadecimal is especially easy because each hex digit maps to exactly 4 binary digits (one "nibble"). To convert, simply group the binary digits into sets of four from the right and replace each group with its hex equivalent.
Example: Convert 101101101001₂ to hexadecimal
| Binary Group | 0001 | 0110 | 1101 | 0001 |
|---|---|---|---|---|
| Hex Digit | 1 | 6 | D | 1 |
Wait — let me regroup correctly. Starting from the right: 1011 0110 1001
| Binary Group | 1011 | 0110 | 1001 |
|---|---|---|---|
| Hex Digit | B | 6 | 9 |
Result: 101101101001₂ = B69₁₆
The reverse works the same way — replace each hex digit with its 4-bit binary equivalent:
Example: Convert 3A7₁₆ to binary
| Hex Digit | 3 | A | 7 |
|---|---|---|---|
| Binary | 0011 | 1010 | 0111 |
Result: 3A7₁₆ = 001110100111₂ (leading zero can be dropped: 1110100111₂)
The Octal Number System
Octal (base-8) uses digits 0 through 7. Each octal digit represents exactly 3 binary digits, making octal-to-binary conversion straightforward — though less commonly used today than hexadecimal.
Binary to Octal: Group binary digits into sets of 3 from the right.
Example: Convert 10110100₂ to octal
| Binary Group | 010 | 110 | 100 |
|---|---|---|---|
| Octal Digit | 2 | 6 | 4 |
Result: 10110100₂ = 264₈
Decimal to Octal: Use repeated division by 8.
Example: Convert 156 to octal
| Step | Divide | Quotient | Remainder |
|---|---|---|---|
| 1 | 156 ÷ 8 | 19 | 4 |
| 2 | 19 ÷ 8 | 2 | 3 |
| 3 | 2 ÷ 8 | 0 | 2 |
Result: 156₁₀ = 234₈
Octal is still used today primarily in Unix/Linux file permissions. The command chmod 755 file means:
- 7 (owner) = 111₂ = read + write + execute
- 5 (group) = 101₂ = read + execute
- 5 (others) = 101₂ = read + execute
Conversion Cheat Sheet
Here is a complete reference table showing values from 0 to 31 across all four common number systems:
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 00000 | 0 | 0 |
| 1 | 00001 | 1 | 1 |
| 2 | 00010 | 2 | 2 |
| 3 | 00011 | 3 | 3 |
| 4 | 00100 | 4 | 4 |
| 5 | 00101 | 5 | 5 |
| 6 | 00110 | 6 | 6 |
| 7 | 00111 | 7 | 7 |
| 8 | 01000 | 10 | 8 |
| 9 | 01001 | 11 | 9 |
| 10 | 01010 | 12 | A |
| 11 | 01011 | 13 | B |
| 12 | 01100 | 14 | C |
| 13 | 01101 | 15 | D |
| 14 | 01110 | 16 | E |
| 15 | 01111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 17 | 10001 | 21 | 11 |
| 18 | 10010 | 22 | 12 |
| 19 | 10011 | 23 | 13 |
| 20 | 10100 | 24 | 14 |
| 21 | 10101 | 25 | 15 |
| 22 | 10110 | 26 | 16 |
| 23 | 10111 | 27 | 17 |
| 24 | 11000 | 30 | 18 |
| 25 | 11001 | 31 | 19 |
| 26 | 11010 | 32 | 1A |
| 27 | 11011 | 33 | 1B |
| 28 | 11100 | 34 | 1C |
| 29 | 11101 | 35 | 1D |
| 30 | 11110 | 36 | 1E |
| 31 | 11111 | 37 | 1F |
Quick conversion rules to remember:
- Binary ↔ Hex: Group/ungroup bits in sets of 4. Each hex digit = 4 binary digits.
- Binary ↔ Octal: Group/ungroup bits in sets of 3. Each octal digit = 3 binary digits.
- Decimal → Any base: Repeated division by the target base, read remainders bottom to top.
- Any base → Decimal: Multiply each digit by its positional weight and sum.
- Hex → Binary → Decimal: Convert hex to binary first, then binary to decimal — often easier than dividing by 16 mentally.
Practicing these conversions until they become second nature will make networking, programming, and system administration tasks significantly easier.