2.4 Number Conversion

Conversion Between Number Systems

Understanding how to convert between different number systems is essential in computer science. Here we'll cover the most common conversion methods.

1. Decimal to Binary Conversion

To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainders:

Example: Convert 2510 to binary

25 ÷ 2 = 12 remainder 1  (LSB)
12 ÷ 2 = 6  remainder 0
6 ÷ 2  = 3  remainder 0
3 ÷ 2  = 1  remainder 1
1 ÷ 2  = 0  remainder 1  (MSB)

Reading remainders from bottom to top: 110012
                    

2. Binary to Decimal Conversion

Multiply each bit by 2 raised to the power of its position (starting from 0 on the right), then sum the results:

Example: Convert 10112 to decimal

1 × 2³ = 1 × 8 = 8
0 × 2² = 0 × 4 = 0
1 × 2¹ = 1 × 2 = 2
1 × 2⁰ = 1 × 1 = 1
               ---
                 1110
                    

3. Decimal to Hexadecimal

Divide the decimal number by 16 and record the remainders (using letters A-F for remainders 10-15):

Example: Convert 25510 to hexadecimal

255 ÷ 16 = 15 remainder 15 (F)
15 ÷ 16  = 0  remainder 15 (F)

Reading remainders from bottom to top: FF16
                    

4. Hexadecimal to Binary

Convert each hexadecimal digit to its 4-bit binary equivalent:

Example: Convert A316 to binary

A = 1010
3 = 0011

Combined: 101000112
                    

5. Binary to Hexadecimal

Group binary digits into sets of four (starting from the right), then convert each group to its hexadecimal equivalent:

Example: Convert 110101102 to hexadecimal

1101 0110
 D    6

Result: D616
                    

Conversion Tips and Tricks

Using Intermediate Bases

When converting between non-decimal bases, it's often easier to first convert to decimal as an intermediate step:

Octal → Decimal → Binary

Hexadecimal → Decimal → Binary

Powers of 2

Memorizing powers of 2 can make conversions much faster:

PowerValuePowerValue
20128256
21229512
2242101,024
23821665,536
24162201,048,576
271282301,073,741,824