General Base Conversions

Converting to base10 

In the previous block, we broke down our every day base10 system into columns, multiplied by a power of 10, then added together. With this method, we can build systems using any number for a base by just changing the powers. For example, let's examine a ternary (base3) numbering system. In a base3 system, each column represents a power of 3 (rather than 10, as in the decimal system). Also, the system has a radix of 3, meaning each column can take on only 3 different values: 0, 1, or 2.

ex. Write the base3 number 2123212_{3} in the decimal (base10) system:

Download file Copy to clipboard
(the right-hand-side of the equations are all in base10)
2123=(232)+(131)+(230)212_{3} = (2 * 3^2) + (1 * 3^1) + (2 * 3^0)
2123=(29)+(13)+(21)212_{3} = (2 * 9) + (1 * 3) + (2 * 1)
2123=1810+310+210212_{3} = 18_{10} + 3_{10} + 2_{10}
2123=2310212_{3} = 23_{10}

So the number 2123212_{3} , a number written using 3 as a base, is the equivalent of 231023_{10} in the base10 system.

Converting a non-decimal system to base10 is straight-forward:

  1. In the base10 system (where we're used to doing our math), multiply each digit from the number in the alternate base (base3 in the above example) by the power of the base that's appropriate for that column.

  2. Add the products together (still in base10). The result is the number that was originally written in an alternate base, now written in base10.

Converting from base10 

The exact same process for converting to base10 can be used to convert from base10 to another base. However, all of the math on the right-hand-side ( multiplication and addition) would need to be done in the alternate base system for it to work. So while it's possible... our brains are used to doing math in base10, so usually use subtraction instead. Here's an abstract step-by-step (we'll take a look at an example right after so it makes more sense):

  1. Find the highest multiple * power of the base that will fit into the base10 number.

  2. Write the alternate base number in the correct column, leaving blanks to the right of it (to fill in later steps).

  3. Subtract the amount equivalent to the alternate base number from your current base10 number. This is now your remainder.

  4. Jump back to step 1, using this remainder instead of your original number.

  5. Repeat steps 1-4 until your remainder is 0.

ex. Write the decimal number 23510235_{10} in a base4 system.

Find the highest mulitple * power that will fit in 23510235_{10} . In base10, 44=2564^4 = 256 , which is too high. So we'll start 43=644^3 = 64 for our power, then 3 for our multiple- so we get 343=1923 * 4^3 = 192 . Remember, because it's a base4 system, the radix is 4, meaning that each column can only take on 4 possible values: 0, 1, 2, 3.

We'll place a 3 in the 434^3 column, which is the 4th one to the left. The remaining places we'll leave blank

Download file Copy to clipboard
23510=3___4235_{10} = 3\_\_\__{4}

Now we can subtract 192 from our original 235 , since we're accounting for that 192 with the 3 in the 4th column of our new base4 number.

Download file Copy to clipboard
2351019210=4310235_{10} - 192_{10} = 43_{10}

Using our remainder of 431043_{10} , jump back to step 1 and continue the process. The next column is 43=164^3 = 16 , and the highest multiplier of 161016_{10} that will fit into 431043_{10} is 2: 162=3216 * 2 = 32 . So we'll put a 2 into the 424^2 column.

Download file Copy to clipboard
23510=32__4235_{10} = 32\_\__{4}

Subtract our newest multiplier from our working remainder:

Download file Copy to clipboard
43103210=111043_{10} - 32_{10} = 11_{10}

So our new remainder is 11. Repeating the process again, our next column's power of 4 is 1 . The highest multiplier is 2 ( 24=82 * 4 = 8 ):

Download file Copy to clipboard
23510=322_4235_{10} = 322\__{4}

Calculate our new remainder:

Download file Copy to clipboard
1110810=31011_{10} - 8_{10} = 3_{10}

With the new remainder of 3, and the final column being essentially a 1's column, we can fill in the final column with just 3 :

Download file Copy to clipboard
23510=32234235_{10} = 3223_{4}

So 23510235_{10} written in base4 is 322343223_{4}

It might seem like a lot of work, but luckily, most of a programmer's work in other number systems involves only a handful of other bases. The more you work in these other base systems, the easier it is to convert back and forth. The two most common systems that you'll see while programming are binary, a base2 system, and hexadecimal, a base16 system. Other common systems are base8 (octal) and base64.