HomePosts

Converting From Decimal to Base b

Converting From Decimal to Base b
Like Tweet Pin it Share Share Email

Reversing the Conversion Process

In the previous post, we discussed how to convert from base b to decimal (base 10). Besides some basic bookkeeping/organization, the rest of the algorithm was mostly multiplication and addition. For example, we converted the hexadecimal number F021A to base 10:

\begin{aligned}  F021A_6 &= F\cdot16^{4} + 0\cdot16^{3} + 2\cdot16^{2} + 1\cdot16^{1} + A\cdot16^{0}\\  &= 15\cdot16^{4} + 0\cdot16^{3} + 2\cdot16^{2} + 1\cdot16^{1} + 10\cdot16^{0}\\  &= 983,578  \end{aligned}

From a mathematical perspective, you might predict that the steps to reverse this process (converting 983,578 from base 10 to hexadecimal) will involve a lot of division and subtraction. After all, the inverse operation of multiplication is division and the inverse operation of addition is subtraction. Let’s try it out.

Remember that a hexadecimal (base 16) representation of the decimal (base 10) number 983,578 will be a sequence of digits, each of which is a coefficient for a power of 16. If we want to find the leftmost digit of the hexadecimal representation of 983,578, we need to know the largest integer power, k of 16 such that 16^k \leq 983,578. You can use mental math, guess-and-check, or the integer part of log_{16}(983,578) to find out that our desired power of 16 is 16^4. Next, perform long division to see how many times 16^4 = 65,536 goes into 983,578:

dec_to_hex_d1

The quotient from this division is 15, which is a base 10 representation of the first digit of our hexadecimal number. We can easily convert 15 in decimal to F in hexadecimal. We now have our first digit of our hexadecimal number! This is a five digit number, F \square \square \square \square, but we still have to fill in these last four squares.

Next, notice that the remainder from our division is 538. You can think of this as the remaining portion of 983,578 that has not yet been converted to hexadecimal yet.

We just need to repeat this process on 538. First, we check if 16^3 \leq 538, which it most certainly is not. This means that our next hexadecimal digit after F is 0 since there are no 16^3 that can divide 538. Now, we’ve gotten our number to F0 \square \square \square.

Next, check if 16^2 \leq 538, which it is. So we can repeat our long division step from before:

dec_to_hex_d2

The quotient from this division is 2, which is our next hexadecimal digit. Our number is almost filled in, F02 \square \square. Notice that the remainder from this division step is 26, which you can think of as the portion of our original number that still hasn’t been converted to hexadecimal. Repeat this process on our remainder of 26 by first checking if 16^1 \leq 26 (which it is). Repeat our long division step yet again:

dec_to_hex_d3

If you are starting to find this process repetitive, then you are getting the idea. This is an algorithm! Our next hexadecimal digit is the quotient from our most recent division step, 1. There’s only one last square to fill in: F021 \square.

The remainder from the above division step is 10. Since it is clear that $16^0 \leq 10$, we just need to perform a final long division step:

dec_to_hex_d4

Our last digit is 10 when described in base 10, or A in hexadecimal. Our number is F021A, as expected. Note that the remainder of 0 in our last division step indicates that it is time to terminate this process.

We should probably do another example, but with less wordiness. The main thing to notice is that this algorithm is just repeated long division. The quotient from each long division represents a hexadecimal digit, and the remainder is the number that we use when we repeat the process.

Converting to Octal (Base 8)

This time, we are going to convert the number 961 from decimal (base 10) to octal (base 8). We first need to find the largest value of k such that 8^k \leq 961. Guess-and-check or computing \lfloor log_8 (961) \rfloor quickly reveals that 8^3 = 512 is the largest power of 8 that is less than 961. We are expecting a 4 digit number to describe 961 in octal (base 8).

Use long division to determine the first digit of our octal number:

dec_to_oct_d1

Our number (so far) is 1 \square \square \square. Use the remainder of 449 for our next division with a divisor of 8^2:

dec_to_oct_d2

Now our number looks like 1 7 \square \square. Use the remainder of 1 for our next division with a divisor of 8^1:

dec_to_oct_d3

Since 8^1 > 1, you can see why this division step has a quotient of 0. Now our octal (base 8) number looks like 1 7 0 \square. Just one more digit to go! Use $1$ again for our next division with a divisor of 8^0:

dec_to_oct_d4

Finally, we have 961_{10} = 1701_8.

Is There an Easier Way?

This technique relies heavily on long division. But if you want to write a computer program that converts a decimal number to base b, you might wonder if there’s an easy way to “do long division” in a computer. The answer is yes. Let’s repeat our conversion 961_{10} = 1701_8 one more time. This time, we will try to use our knowledge of computers to make this process a bit easier. Instead of performing long division, you just need to find the quotient and remainder from this process. You can find a quotient of a \div b by asking the computer for the integer part of a/b. For our pseudocode, we will let div represent the integer part of our division (also known as integer division). Similarly, you can find the remainder of the division a \div b by finding a \textup{ mod } b. For our pseudocode, we will write this as a % b.

  1. first exponent of 8: k = integer part of log(961)/log(8) = 3
  2. first digit: d3 = 961 div 8^3 = 1 and remainder: r3 = 961 % 8^3 = 449
  3. second digit:d2 = 449 div 8^2 = 7 and remainder: r2 = 449 % 8^2 = 1
  4. third digit: d1 = 1 div 8^1 = 0 and remainder: r1 = 1 % 8^1 = 1
  5. fourth digit: d0 = 1 div 8^0 = 1 and remainder: r0 = 1 % 8^0 = 0
  6. result: d3d2d1d0 = 1701

Converting to Binary (Base 2)

This process will work for any integer number base b>1. So we should be able to use it to convert to binary (base 2). Let’s convert the decimal number 38 to binary.

  1. first exponent of 2: k = integer part of log(38)/log(2) = 5
  2. first digit: d5 = 38 div 2^5 = 1 and remainder: r5 = 45 % 2^5 = 6
  3. second digit:d4 = 6 div 2^4 = 0 and remainder: r4 = 6 % 2^4 = 6
  4. third digit: d3 = 6 div 2^3 = 0 and remainder: r3 = 6 % 2^3 = 6
  5. fourth digit: d2 = 6 div 2^2 = 1 and remainder: r2 = 6 % 2^2 = 2
  6. fifth digit: d1 = 2 div 2^1 = 1 and remainder: r1 = 2 % 2^1 = 0
  7. sixth digit: d0 = 0 div 2^0 = 0 and remainder: r0 = 0 % 2^0 = 0
  8. result: d5d4d3d2d1d0 = 100110

Hence, we have that 38_{10} = 1001100_2. If you are not a fan of this pseudocode calculation, you can still use long division to get the same result:

dec_to_bin_d1
dec_to_bin_d2
dec_to_bin_d3
dec_to_bin_d4
dec_to_bin_d5
dec_to_bin_d6

Again, we see that 38_{10} = 1001100_2.

An Algorithm to Convert Decimal (base 10) to base b

Now that you have a pretty good idea of how to convert from base b to base 10, how would you write a computer program to complete this task for you? The steps that your program will need to follow is called an algorithm. In general, it is a good idea to

  1. Write the steps of your algorithm in your own words (in plain English). Try your steps out on an example and see if you missed anything.
  2. Write out the steps of your algorithm in pseudocode. Pay particular attention what your function takes as input, what it produces as output, and what steps need to happen in between to turn your input into your desired output. Again, try out your steps on an example and see if you missed anything.
  3. Convert your pseudocode to actual code in your language of choice. Make sure to put comments in your code. You should also spend some time testing what you created. Does your algorithm produce the expected output for some simple calculations? Finally, test the limits of your code. Does your program handle user input that doesn’t make sense or somehow “breaks the rules”? For example, you should not be able to convert a number from base 1 since this number base doesn’t exist!

Practice Problems

Here are a few practice problems to try on your own:

Convert 103 from decimal (base 10) to binary (base 2).

  1. first exponent of 2: k = integer part of log(103)/log(2) = 6
  2. first digit: d6 = 103 div 2^6 = 1 and remainder: r6 = 103 % 2^6 = 39
  3. second digit: d5 = 39 div 2^5 = 1 and remainder: r5 = 39 % 2^5 = 7
  4. third digit:d4 = 7 div 2^4 = 0 and remainder: r4 = 7 % 2^4 = 7
  5. fourth digit: d3 = 7 div 2^3 = 0 and remainder: r3 = 7 % 2^3 = 7
  6. fifth digit: d2 = 7 div 2^2 = 1 and remainder: r2 = 7 % 2^2 = 3
  7. sixth digit: d1 = 3 div 2^1 = 1 and remainder: r1 = 3 % 2^1 = 1
  8. seventh digit: d0 = 1 div 2^0 = 1 and remainder: r0 = 1 % 2^0 = 0
  9. result: d6d5d4d3d2d1d0 = 1100111

Convert 179 from decimal (base 10) to octal (base 8).

  1. first exponent of 8: k = integer part of log(179)/log(8) = 2
  2. first digit: d2 = 179 div 8^2 = 2 and remainder: r2 = 179 % 8^2 = 51
  3. second digit: d1 = 51 div 8^1 = 6 and remainder: r1 = 51 % 8^1 = 3
  4. third digit: d0 = 3 div 2^0 = 3 and remainder: r0 = 3 % 8^0 = 0
  5. result: d2d1d0 = 263

Convert 1997 from decimal (base 10) to hexadecimal (base 16).

  1. first exponent of 16: k = integer part of log(1997)/log(16) = 2
  2. first digit: d2 = 1997 div 16^2 = 7 and remainder: r2 = 1997 % 16^2 = 205
  3. second digit: d1 = 205 div 16^1 = 12 (or C in hex) and remainder: r1 = 205 % 16^1 = 13
  4. third digit: d0 = 13 div 16^0 = 13 (or D in hex) and remainder: r0 = 13 % 16^0 = 0
  5. result: d2d1d0 = 7CD

Next Steps

Now that you are comfortable with converting from base 10 to base b and from base b to base 10, you may want to read my next post about how to easily convert between binary and hexadecimal. The title of this post is Bits, Bytes, and Binary.

Finally, if you want some more practice, you can make up your own problemas using this number base conversion tool.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *