Language: EN

conversion-entre-bases-en-binario

How to convert numbers between bases

In the previous post, we talked about numerical systems, number representation, and positional systems.

Thus, the concept of base appeared, which is the set of symbols we are going to use to represent numbers. More specifically, we will not usually refer to the number of available symbols.

So we saw an invented numerical system that had this base 🔵,🔺, 🟩,⭐. Now it’s time for a little bit of math to “settle” the concepts.

Note: during this post I will use the decimal base as the “normal” system. Basically because if we talk in stars and squares, it would be difficult to understand. But remember that everything would be equally valid with any other system.

Mathematical content ahead! If you don’t feel like reading math right now, go for another post and come back in a while.

How much is my number in baseN?

Returning to our example of base 🔵,🔺, 🟩,⭐ (1, 2, 3, 4) What equivalence to the decimal base does a number have?

🌟

In this case, we have a baseN = 4

  • The right digit can take values from 0 to 3
  • If there is a digit in the next position, it means that the one on the right has gone through 4
  • If there is another digit in the next position, it means that it has gone through 4 times by 4
  • And so on…

Examples

That is, if we have the number 🔺🟩 (1, 2)

  • On the right, I have a 2
  • On the left, I have a 1, which means that the one on the right has gone through 4 positions

That is, the number I have is

4 + 2 = 6

If the number were 🟩🟩 (2, 2), it would have

  • On the right, I have a 2
  • On the left, I have a 2, which means that the one on the right has passed 2 times through 4 positions

So the number I have is

(2 * 4) + 2 = 10

And if I keep clicking until I have 3 digits 🔺🟩🟩 (1, 2, 2)

  • On the right, I have a 2
  • In the middle, I have a 2, which is worth 4 x 2
  • On the left, I have a 1, which is worth 4 x 4
(1 * 4²) + (2 * 4) + 2 = 10

BaseN to decimal conversion

So how do we convert from BaseN to decimal? It is what we have done above, but generalized to any base.

Being,

  • Dᵢ the digit at position i
  • Base, the number of symbols in your base

For example, to convert the number 2.310 in base 4 to decimal.

base₁₀  = 2 * 4³  + 3 * 4²  + 1 * 4¹  + 0 * 4
        = 128      + 48       + 4        + 0
        = 180

This equation is also equivalent to this sequence

Decimal to BaseN conversion

Now let’s see the reverse process, changing a decimal number to a BaseN. In this case, it is easiest to divide and calculate remainders.

Let’s see an example, convert 180 to base 4.

base=> 180 / 4 = 45,   remainder 0
          45 / 4 = 11,   remainder 1
          11 / 4 = 2,    remainder 3
           2 / 4 = 0     remainder 2

And your decimal number 180 converted to base 4, are the ordered remainders, 2310.

Comparison with decimal

Does all that seem strange to you? If it has been difficult for you to visualize, think for a moment about the decimal system.

0

Imagine the number 1,537. What we have said is that, we can think of 1,527 as a sum of powers of 10.

1.537 = 1.000 + 500 + 30 + 7

//which is the same as saying
1.537 = (1 * 10³) + (5 * 10²) + (3 * 10) + (7)

You do these operations continuously! It’s just that you have the decimal system very internalized.

We have also said that, instead of thinking of it as a sum of powers, you can think of it as a sequence. At each step you multiply the previous step by 10, and add a number.

1.537 = 153 * 10  + 7
      = (15 * 10 + 3) * 10 + 7
      = (1 * 10 + 5) * 10 + 3) * 10 + 7

Finally, you can also do the sequence in reverse, and keep the remainders

base10 => 1.537 / 10 = 153,  remainder 7
	        153 / 10 = 15,   remainder 3
            15 / 10 = 1,    remainder 5
             1 / 10 = 0     remainder 1

They are the same operations that you have done for the base change, only instead of ‘10’ you have put BaseN.