Number Bases and Conversion

Human beings are most comfortable or familiar with counting using the numbers 0 – 9. This is also referred to as the Base-10 system. We call it the Base-10 system because there are exactly 10 symbols or numbers/digits represented.

From an early age we are taught to use these numbers and after some time it becomes second nature. This is why Base-10 feels so natural to us. Base-10 systems have existed for a very long time too. Many civilizations throughout history have used a Base-10 numbering system. In fact, it is widely accepted that the reason we use Base-10 and not some other arbitrary number system is because humans have 10 fingers!

What is a Number Base (Radix)?

A number base, radix, or “base” for short is a system of counting that describes numbers using symbols (such as numbers and letters) to represent values. The base of a number system also tells us how many unique number notations exist.

For example, Base-2 or the binary numbering system has exactly 2 unique numbers used to express different values (0 and 1). Binary is used in programming and this makes sense because computers “think” in binary. This isn’t the only number base that we often see in programming.

Another example of a programming-friendly number base is Hexadecimal or Base-16. As you may have already figured out, in this number base we have 16 unique symbols to represent different values. They are the digits 0 – 9 and the letters A – F.

Example: How many symbols/digits exist in Base-8 (Octal)?

  • 8 – The numbers 0 – 7

When working with numbers in different bases we need to be clear about which base we are representing. One way we do this is by using a simple subscript next to the number we are working with. For example, 1002 is in Base-2 or the number 410 in decimal. 10010 is in Base-10 or the number “One Hundred”. 10016 is in Base-16 or the number 25610 in decimal. Each value very different from the others. If no base is specified, we generally assume Base-10 as the default.

Counting in Different Number Bases

Counting in another number base is very similar to counting in our more familiar Base-10. In decimal or Base-10 we can count 0, 1, 2, … 7, 8, 9, and then 10. Notice how in Base-10 there is no unique symbol/digit for the value “10”. Instead, ten is made up of the digits 1 and 0. This is also true for all other bases.

For example, Base-2 doesn’t contain the symbol “2” but rather the idea of “2” is represented by “10” (1 and 0. Not the number 10, or ten).

Another thing to note is how when we reach the last digit in our number system, we reset, and shift/carry. For example, in decimal 9 + 1 = 10. Adding 1 to the last symbol/digit (9) in our number system (Base-10) resets the “One’s” place to 0. We also shift or carry a 1 over to the left in what we call the “Ten’s” place. This gives us 1 – Ten and 0 – Ones, or Ten. If we continue to shift left we get to the “Hundred’s” place, then the “Thousand’s” place and so on.

Every time we shift to the left the value of our number is effectively multiplied by the base we are operating in. In Computer Science, this operation actually has a name. We call it an Arithmetic Shift (to the left). Arithmetic Left Shifts are equivalent to multiplication by the radix or base we are operating in.

Following this logic what happens when we add or count in Base-2 (binary)? Since binary only has the symbols 0 and 1, we will need to reset, shift/carry after “1”. In binary, 1 + 1 = 10 (One Zero. Not to be confused with the number Ten in decimal). As we do in decimal-land, adding 1 to the last symbol/digit (1) in our number system (Base-2) resets the “One’s” place to a 0 and we shift or carry a 1 over to the left which we call the “Two’s” place. Why the “Two’s” place? Well, because we are counting in Base-2. Every shift or carry over to the left is a power of that base. So in binary, we have the 1’s (20), 2’s (21), 4’s (22), 8’s (23) place and so on. Remember that every time we shift to the left we are effectively multiplying by the base or radix which we are operating in.

So far we’ve seen what counting in a base lower than 10 looks like. What does counting in a base greater than 10 look like? Something like Hexadecimal or Base-16?

Let’s start and see where it takes us. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, … What comes after “F”? If counting in hexadecimal is anything like counting in binary or decimal then F + 1 should be similar to 1 + 1 in binary or 9 + 1 in decimal. Indeed it is! Again, adding 1 to the last symbol/digit (F) in our number system (Base-16) resets the “One’s” place to a 0 and we shift or carry a 1 over to the left which we call the “16’s” place.

Converting between Number Bases

We already have some good clues that will help us in converting between number bases. Namely, we now know that whenever we reach the highest single digit/value in a column or place, we reset and shift or carry to the left, which we determined was equivalent to a multiplication by that base. Depending on the base we are operating in, that number column or place will be some power of that base. This little nugget of information will help us convert a number in ANY base to its equivalent value in Base-10. What follows are a few examples.

Base 2:

  • From right to left we have the 1’s column, the 2’s column, the 4’s column, the 8’s column, the 16’s column, etc… Notice how each column is 1 power greater than the column before it. The “X’s” represent the symbol/digit of the number.
  • (x5 * 24) + (x4 * 23) + (x3 * 22) + (x2 * 21) + (x1 * 20)
  • (x5 * 16) + (x4 * 8) + (x3 * 4) + (x2 * 2) + (x1 * 1)
  • For example, 11001 would be expressed:
    • (1 * 24) + (1 * 23) + (0 * 22) + (0 * 21) + (1 * 20)
    • (1 * 16) + (1 * 8) + (0 * 4) + (0 * 2) + (1 * 1)
    • 110012 = 1610 + 810 + 110 = 2510

Base 10:

  • Similarly, in the decimal system from right to left we have the 1’s column, the 10’s column, the 100’s column, the 1000’s column, the 10000’s column, etc… Each column 10 times greater than the column before it.
  • (x5 * 104) + (x4 * 103) + (x3 * 102) + (x2 * 101) + (x1 * 100)
  • (x5 * 10000) + (x4 * 1000) + (x3 * 100) + (x2 * 10) + (x1 * 1)
  • For example, 12705 would be expressed:
    • (1 * 104) + (2 * 103) + (7 * 102) + (0 * 101) + (5 * 100)
    • (1 * 10000) + (2 * 1000) + (7 * 100) + (0 * 10) + (5 * 1)
    • 1270510 = 1000010 + 200010 + 70010 + 010 + 510 = 1270510

Base 16:

  • Following the same formula in Base-16; starting from the right and going to the left, we have the 1’s column, the 16’s column, the 256’s column, the 4096’s column and the 65536’s column and so on … ever increasing 16 times whenever we shift left.
  • (x5 * 164) + (x4 * 163) + (x3 * 162) + (x2 * 161) + (x1 * 160)
  • (x5 * 65536) + (x4 * 4096) + (x3 * 256) + (x2 * 16) + (x1 * 1)
  • For example, E117E would be expressed:
    • (E * 164) + (1 * 163) + (1 * 162) + (7 * 161) + (E * 160)
    • (E * 65536) + (1 * 4096) + (1 * 256) + (7 * 16) + (E * 1)
    • Remember the hexadecimal symbols A – F represent the decimal numbers 10 – 15.
    • (14 * 65536) + (1 * 4096) + (1 * 256) + (7 * 16) + (14 * 1)
    • E117E16 = 91750410 + 409610 + 25610 + 11210 + 1410 = 92198210

We can more generally express this as the following:

Converting a Number from Base-X to Base-10

  • Let’s say we have a number – 123XYZB (Base-B)
  • Generally, we take the symbol/digit and multiply by the base (B) raised to the power of the column or place that symbol/digit is in. The columns/places start from 0 and the rightmost symbol/digit of the number. Finally, we add all of our products together and get our decimal equivalent number.
  • (1 * B5) + (2 * B4) + (3 * B3) + (X * B2) + (Y * B1) + (Z * B0)

Converting a Number from Base-10 to Base-X

What about going the other way around? That is, taking a number we have in Base-10 and converting it to another base? Let’s take the number 35 and convert it into Binary (Base-2), Octal (Base-8) and Hexadecimal (Base-16).

As is true with many things in math, there are multiple ways to approach this task. One common and simple method involves using Modulo Division. Unlike regular division, Modulo Division or Modulus gives us just the remainder of a division operation rather than the quotient and remainder. For example, 10/3 = 3r1 or 3 remainder 1. However, 10%3 = 1. Also expressed as 10 mod 3 = 1. Using this method of Modulo Division we can convert any number from Base-10 to whatever other Base-X we choose. The idea behind this method is somewhat the reverse of what we did when converting a number of Base-X to Base-10. We are going to find how many times our original number can be divided into by our desired Base (X) and use that result as the number we divide into (dividend) in the following step. We also determine what the remainder of that division (Modulus) is while keeping track of our results. We repeat these steps until we reach a dividend of 0 at which point we calculate the Modulus one last time. We then piece together the results of our Modulo Division operations in reverse order (starting from our last result to our first result) to get our converted number.

Example: What is 35 in Binary?

  • Binary is Base-2 so we will be dividing by 2.
  • 35/2 = 17 and 35%2 = 1
  • Repeat until the dividend is 0.
  • 17/2 = 8 and 17%2 = 1
  • 8/2 = 4 and 8%2 = 0
  • 4/2 = 2 and 4%2 = 0
  • 2/2 = 1 and 2%2 = 0
  • 1/2 = 0 and 1%2 = 1
  • In the following step our dividend is 0 so we stop.
  • Putting the results of our Modulus Division operations all together in reverse order we get:
  • 1000112 = 3510

Example: What is 35 in Octal?

  • Octal is Base-8 so we will be dividing by 8.
  • 35/8 = 4 and 35%8 = 3
  • Repeat until the dividend is 0.
  • 4/8 = 0 and 4%8 = 4
  • In the following step our dividend is 0 so we stop.
  • Putting the results of our Modulus Division operations all together in reverse order we get:
  • 438 = 3510

Example: What is 35 in Hexadecimal?

  • Hexadecimal is Base-16 so our divisor will be 16.
  • 35/16 = 2 and 35%16 = 3
  • Repeat until the dividend is 0.
  • 2/16 = 0 and 2%16 = 2
  • In the following step our dividend is 0 so we stop.
  • Putting the results of our Modulus Division operations all together in reverse order we get:
  • 2316 = 3510

Example: What is 672 in Hexadecimal?

  • Hexadecimal is Base-16 so our divisor will be 16.
  • 672/16 = 42 and 672%16 = 0
  • Repeat until the dividend is 0.
  • 42/16 = 2 and 42%16 = 1010 or A16 (Remember that in Base-16 we use the symbols A – F to represent the numbers 10 – 15)
  • 2/16 = 0 and 2%16 = 2
  • In the following step our dividend is 0 so we stop.
  • Putting the results of our Modulus Division operations all together in reverse order we get:
  • 2A016 = 67210

Converting a Number from Base-X to Base-Y

Now that we’re comfortable converting a number to and from Base-10 it will be easy for us to convert a number from any base to any other base. All that is required is for us to convert our original number into Base-10 and then from Base-10 to the desired base.

Example: What is 7358 (Base-8) in Base-5?

  • First we convert 7358 (Base-8) to Base-10:
    • 7358 is expressed:
    • (7 * 82) + (3 * 81) + (5 * 80)
    • (7 * 64) + (3 * 8) + (5 * 1)
    • 7358 = 44810 + 2410 + 510 = 47710
  • Next, we convert our result from step 1 (47710) into our desired base (Base-5).
    • Our desired base is Base-5 so we will be dividing by 5.
    • 477/5 = 95 and 477%5 = 2
    • Repeat until the dividend is 0.
    • 95/5 = 19 and 95%5 = 0
    • 19/5 = 3 and 19%5 = 4
    • 3/5 = 0 and 3%5 = 3
    • In the following step our dividend is 0 so we stop.
    • Putting the results of our Modulus Division operations all together in reverse order we get:
    • 34025 = 47710

Finally, we arrive at our solution:

  • 7358 = 34025