Converting Between Number Systems

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

StepDivideQuotientRemainder
1156 ÷ 2780
278 ÷ 2390
339 ÷ 2191
419 ÷ 291
59 ÷ 241
64 ÷ 220
72 ÷ 210
81 ÷ 201

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

StepMultiplyInteger PartRemaining Fraction
10.8125 × 210.625
20.625 × 210.25
30.25 × 200.5
40.5 × 210.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

Position76543210
Binary Digit11010110
Power of 21286432168421
Value128640160420

Sum: 128 + 64 + 16 + 4 + 2 = 214₁₀

A shortcut for common byte values is to memorize the powers of 2:

Power2⁰2⁴2⁵2⁶2⁷2⁸2⁹2¹⁰
Value12481632641282565121024

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.

Decimal0123456789101112131415
Hex0123456789ABCDEF

Example: Convert 4392 to hexadecimal

StepDivideQuotientRemainderHex Digit
14392 ÷ 1627488
2274 ÷ 161722
317 ÷ 16111
41 ÷ 16011

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

StepDivideQuotientRemainderHex Digit
1255 ÷ 161515F
215 ÷ 16015F

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

PositionHex DigitDecimal ValuePower of 16Result
32216³ = 40968192
2F1516² = 2563840
11116¹ = 1616
0A1016⁰ = 110

Sum: 8192 + 3840 + 16 + 10 = 12,058₁₀

Common hex values you should know:

HexDecimalBinaryCommon Use
0x0A1000001010Line feed (LF)
0x0D1300001101Carriage return (CR)
0x203200100000Space character
0x304800110000ASCII '0'
0x416501000001ASCII 'A'
0x619701100001ASCII 'a'
0x7F12701111111DEL character
0xFF25511111111Max 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 Group0001011011010001
Hex Digit16D1

Wait — let me regroup correctly. Starting from the right: 1011 0110 1001

Binary Group101101101001
Hex DigitB69

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 Digit3A7
Binary001110100111

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 Group010110100
Octal Digit264

Result: 10110100₂ = 264₈

Decimal to Octal: Use repeated division by 8.

Example: Convert 156 to octal

StepDivideQuotientRemainder
1156 ÷ 8194
219 ÷ 823
32 ÷ 802

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:

DecimalBinaryOctalHex
00000000
10000111
20001022
30001133
40010044
50010155
60011066
70011177
801000108
901001119
100101012A
110101113B
120110014C
130110115D
140111016E
150111117F
16100002010
17100012111
18100102212
19100112313
20101002414
21101012515
22101102616
23101112717
24110003018
25110013119
2611010321A
2711011331B
2811100341C
2911101351D
3011110361E
3111111371F

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.