HomePosts

Converting From Base b to Base 10

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

Counting on Your Fingers

Because humans have ten fingers, most cultures have adopted a decimal (base 10) number system. When we want to group numbers together, we do so by tens. For example, while you might not think of it this way very often, the number 1729 can be expressed a single one thousand, seven one hundreds, two tens, and 9 ones. If you remember that 1000 equals 10^{3}, 100 equals 10^{2}, 10 equals 10^{1}, and 1 equals 10^{0}, then we can write this a bit more mathematically:

1729 = 1\cdot10^{3} + 7\cdot10^{2} + 2\cdot10^{1} + 9\cdot10^{0}

Each of the digits of the base 10 number 1729 appear as the coefficients in our sum, 1\cdot10^{3} + 7\cdot10^{2} + 2\cdot10^{1} + 9\cdot10^{0}. But is there anything special about the number 10? Besides the fact that most people have ten fingers, not especially. One might even daydream about what would happens on distant planet with intelligent life if the planets’ inhabitants have a different number of fingers than us earthlings. For example, this space alien from the cartoon Futurama only has six fingers. Some of you may recognize this character as Lrr, Ruler of the Planet Omicron Persei 8:

lrr

In most number systems used on Earth, we run out of digits to represent numbers after our tenth digit. That is, we can count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. But after 9, we don’t have an extra symbol set aside for the number that comes next, so we progress to 10. Our Omicronian friends may do the same thing, but with six symbols instead of ten. They may start counting the same as us: 0, 1, 2, 3, 4, 5. But after 5, they would run out of symbols and progress to 10. For a human, 10 in our decimal (base 10) number system means 1\cdot10^{1} + 0\cdot10^{0}. But for our space alien, 10 in their senary (base 6) number system means 1\cdot6^{1} + 0\cdot6^{0}. If the space alien continued counting, it would probably look something like this: 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20,....

At this point, you might be getting a bit concerned. Communicating with space aliens sounds like a pretty daunting task. And if we can’t even communicate about math, what hope do we have to establish strong interplanetary relations? One can even imagine the leaders of our two planets accidentally sparking a diplomatic crisis over a misunderstanding related to a \$20 bet. Luckily, it turns out that number bases will be the least of our problems when it comes to intergalactic diplomacy: converting between different number bases is not as hard as it sounds.

Converting Senary (Base 6) to Decimal (Base 10)

Suppose the Omicronians ask the people of Earth to send a box of 2152 of their tastiest chocolate bars. Since we know that the Omicronians use a senary (base 6) number system, we realize that 2152_6 can be broken down as follows:

\begin{aligned}  2152_6 &= 2\cdot6^{3} + 1\cdot6^{2} + 5\cdot6^{1} + 2\cdot6^{0}\\  &= 500  \end{aligned}

So it seems that the Omicronians would like us to send 500 chocolate bars.

This general principle works for any number base. While the Omicronians might use a senary (base 6) number system, what if we encounter an alien race that uses a hexadecimal (base 16) number system?

Converting Hexadecimal (Base 16) to Decimal (Base 10)

Hexadecimal (base 16) might be a bit more difficult for us humans to wrap our heads around because we don’t have enough digits available for such a number system. We can count just fine in base 16 for a while: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. But after 9, things get a bit dicey. While it would feel natural to continue on to 10, this doesn’t make sense because we know that in base 16, 10_{16} = 1\cdot16^1 + 0\cdot16^0. We need some extra symbols for the numbers from 10 through 15. The first few capital letters of the alphabet should suffice:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, ...

Finally, after the number F, we can continue on to 10. Let’s keep counting from F for a bit:

..., F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 21, 22, ...

If the leaders of this planet asked us for F021A chocolate bars, how many would we send? If you wanted, you could start counting, starting at 0 and ending at F021A. But that wouldn’t be very efficient. We can do better. See if you can find the digits F, 0, 2, 1, and A in the first line of work below:

\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}

Notice how we switched F to 15 and A to 10 in our second line of work. After that, the rest of the conversion is just number crunching. We discover that F021A_16 = 983,578_{10}. That’s a lot of chocolate bars.

A General Formula to Convert Base b to Decimal (Base 10)

We can generalize this procedure in a mathematical way. Suppose you have an integer n in base b represented by k+1 digits, n_k n_{k-1} n_{k-2} \cdots n_2 n_1 n_0. You can convert this number to base 10 by performing the following calculation:

n_b = n_k b^{k} + n_{k-1} b^{k-1} + n_{k-2} b^{k-2} + \cdots + n_2 b^{2} + n_1 b^{1} + n_0 b^{0}

If you prefer summation/sigma notation, you can also the write the formula as follows:

n_b = \sum_{i=0}^{k} n_i b^{i}

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

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 23041 from quinary (base 5) to decimal.
\begin{aligned}  23041_5 &= 2\cdot5^{4} + 3\cdot5^{3} + 0\cdot5^{2} + 4\cdot5^{1} + 1\cdot16^{0}\\  &= 1,646  \end{aligned}
Convert 1010 from binary (base 2) to decimal.
\begin{aligned}  1010_2 &= 1\cdot2^{3} + 0\cdot2^{2} + 1\cdot2^{1} + 0\cdot2^{0}\\  &= 10  \end{aligned}
Convert ACE1 from hexadecimal (base 16) to decimal.
\begin{aligned}  ACE1_{16} &= A\cdot16^{3} + C\cdot16^{2} + E\cdot16^{1} + 1\cdot16^{0}\\  &= 10\cdot16^{3} + 12\cdot16^{2} + 14\cdot16^{1} + 1\cdot16^{0}\\  &= 44,257  \end{aligned}

Next Steps

Now that you are comfortable with converting from base b to base 10, you may want to read my next post about how to convert from base 10 to base b.

Finally, if you want some more practice, you may want to check out this number base conversion calculator so you can make up your own problems.

Comments (0)

Leave a Reply

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