Reversing the Conversion Process

To convert from base 10 to base $b$, we use repeated division. The key insight is that converting to decimal involves multiplication and addition, so converting from decimal involves division and remainders.

A number in base $b$ is a sequence of digits, each a coefficient for a power of $b$. To find each digit, we perform integer division by decreasing powers of $b$, tracking quotients and remainders.

Example: Converting 6717 to Hexadecimal (Base 16)

We want to convert $6717_{10}$ to base 16. First, find the largest power $k$ of 16 such that $16^k \leq 6717$. Since $16^3 = 4096$ and $16^4 = 65536$, we have $k = 3$. Our result will be a 4-digit hex number.

first digit:  d3 = 6717 div 4096 = 1    remainder: 6717 % 4096 = 2621
second digit: d2 = 2621 div 256  = 10 (A) remainder: 2621 % 256  = 61
third digit:  d1 = 61   div 16   = 3    remainder: 61   % 16   = 13
fourth digit: d0 = 13   div 1    = 13 (D) remainder: 13   % 1    = 0

Result: $6717_{10} = \text{1A3D}_{16}$

Example: Converting 961 to Octal (Base 8)

Find the largest power: $8^3 = 512 \leq 961$ but $8^4 = 4096 > 961$, so $k = 3$. We expect a 4-digit number.

first digit:  d3 = 961 div 512 = 1   remainder: 961 % 512 = 449
second digit: d2 = 449 div 64  = 7   remainder: 449 % 64  = 1
third digit:  d1 = 1   div 8   = 0   remainder: 1   % 8   = 1
fourth digit: d0 = 1   div 1   = 1   remainder: 1   % 1   = 0

Result: $961_{10} = 1701_8$

Example: Converting 38 to Binary (Base 2)

Since $2^5 = 32 \leq 38$ but $2^6 = 64 > 38$, we have $k = 5$ and expect a 6-digit binary number.

d5 = 38 div 32 = 1   remainder: 38 % 32 = 6
d4 = 6  div 16 = 0   remainder: 6  % 16 = 6
d3 = 6  div 8  = 0   remainder: 6  % 8  = 6
d2 = 6  div 4  = 1   remainder: 6  % 4  = 2
d1 = 2  div 2  = 1   remainder: 2  % 2  = 0
d0 = 0  div 1  = 0   remainder: 0  % 1  = 0

Result: $38_{10} = 100110_2$

The Algorithm

The general algorithm to convert a decimal number $n$ to base $b$:

1. Find k = floor(log(n) / log(b))     // largest power of b ≤ n
2. For i from k down to 0:
     digit[i] = n div b^i              // integer division
     n        = n mod b^i              // remainder becomes new n
3. The result is digit[k] digit[k-1] ... digit[1] digit[0]

In a computer program, you can use div (integer division) and % (modulo) to get the quotient and remainder.

Writing Your Own Program

If you want to write code to do this conversion, follow these steps:

First, write the algorithm in plain English. Then try it on an example to make sure you haven't missed anything. Next, write pseudocode, paying attention to inputs, outputs, and the steps in between. Finally, convert your pseudocode to actual code and test it with simple cases and edge cases (like base 1, which doesn't exist).

Practice Problems

Convert 103 from decimal to binary (base 2)
k = floor(log(103)/log(2)) = 6

d6 = 103 div 64 = 1   r = 103 % 64 = 39
d5 = 39  div 32 = 1   r = 39  % 32 = 7
d4 = 7   div 16 = 0   r = 7   % 16 = 7
d3 = 7   div 8  = 0   r = 7   % 8  = 7
d2 = 7   div 4  = 1   r = 7   % 4  = 3
d1 = 3   div 2  = 1   r = 3   % 2  = 1
d0 = 1   div 1  = 1   r = 1   % 1  = 0

Result: 103 = 1100111 in binary
Convert 179 from decimal to octal (base 8)
k = floor(log(179)/log(8)) = 2

d2 = 179 div 64 = 2   r = 179 % 64 = 51
d1 = 51  div 8  = 6   r = 51  % 8  = 3
d0 = 3   div 1  = 3   r = 3   % 1  = 0

Result: 179 = 263 in octal
Convert 1997 from decimal to hexadecimal (base 16)
k = floor(log(1997)/log(16)) = 2

d2 = 1997 div 256 = 7    r = 1997 % 256 = 205
d1 = 205  div 16  = 12 (C)  r = 205  % 16  = 13
d0 = 13   div 1   = 13 (D)  r = 13   % 1   = 0

Result: 1997 = 7CD in hexadecimal

Next Steps

Now that you're comfortable with converting between bases, practice with the Number Base Conversion Calculator to check your work.