Universal base converter

Why do Programmers confuse Halloween with Christmas?

Because 31 oct = 25 dec!

(Read somewhere over the internet)

A while ago I was doing some school exercises about decimal to binary conversion and this kind of stuff. If you know how this works, you know that doing lots of these conversions by hand can became quite boring after a while. So I decided to code myself a converter. But not just a converter that could do decimal-to-binary and viceversa, but one that could handle all kind of bases I knew.

As my Zip Bruteforcer, this program was made as a command line utility, with the only differences that this time I’ve used Python instead of Java.

Before telling you how I made this, I’d like to remember you that all the code is available on github, as always.

How It Works:

First of all we have to remember a very important thing: in everyday life, the numerical system we use is a base 10 system, this means that it can use all digits from 0 to 9. Generally, every numerical base uses digits in the range 0 up to (base-1).

This means that base 9 system uses digits in the range 0-8, base 8 system uses digits in the range 0-7 and so on.

What about bases greater than 10? For example base 16 (hexadecimal)?

Well, in that case we can use digits from 0 to 9, but we can’t use 10, 11, 12, 13, 14 and 15 as digits, because they’re numbers, made up of more than one digit. To overcome this problem we use letters: so A becomes 10, B becomes 11, C becomes 12 and so on.

The concept behind my converter is really simple:

As we’ll see later in this post, the steps to convert any number into a base 10 number are the same for every base, the only things that change depend on the base.

And it’s the same when you convert from base 10 to any other base.

My program simply converts the starting number into a base 10 number and then converts the result into another given base.

Converting any base into base 10:

First of all, we have to convert the starting number into a base 10 number. This program won’t mind if the base is already 10, this process we’ll be executed anyway. In this article, the base of a number is expressed inside parenthesis at the right of the number.

The conversion is really easy:

Starting from right, you multiply every digit of the number by the base elevated to a certain power. The exponent of this power is 0 for the number at the most right and increases by 1 as you go left. After the multiplication you add up all the results and you get the number converted into base 10.

Watching this image should be much easier to understand.

For example, we can try converting 1001110(2) to a base 10 number.

But this can also be applied to other bases, we can test it using the numbers from the joke in the introduction and check if it’s true:

Now that we verified that 31(8) is really 25(10) we can laugh at the joke.

This is it translated into some Python code

As you can see, this already includes a conversion from letters (A, B, C, D…) to the corresponding number (10, 11, 12, 13…).

Converting from base 10 to other base:

Now that we got a base 10 number, we need to convert into the new base.

When converting from base 10 to some other base, you simply divide the base 10 number by the new base, keeping the division rest and diving the result again by the new base. You  repeat this until the division result is 0. Then you take all the rests you got in order, flip them and you finally have the number expressed in the new base.

Let’s use the same numbers from the previous example:

As I said before, this works for every base, so we can test again if the intro joke is correct:

Now that we check again that the intro joke was correct, we can laugh once more.

Translated into code, it’s something like this:

As you can see, the code does exactly what I said before:

The number to be converted gets divided by the base until the result is 0 and the division rest gets stored in a list. Then the list with the rests is reversed and finally every number greater than or equal to 10 is replaced with the correspondent letter.

Adding special bases

But, when dealing with computers, sometimes you have to use some variants of the binary (base 2) system, such a Two’s Complements (expressed as CP2 is the program), mostly used to express negative binary numbers, and Binary Coded Decimal (expressed as BCD). This two systems simply need different calculations to be used, but they’re included in the number-base10-newbase logic of the converter.

Two’s Complement (CP2)

This is just a way to express signs with binary numbers. After converting a number into a binary number, you add a 0 at the most left. If the binary number has to be positive, the conversion is done; otherwise, if the number has to be negative, another step is required.

Starting from right to left, the number remains unchanged until you get a one. After that one, every number is inverted, so a 1 becomes a 0 and a 0 becomes a 1.

For example, as we saw before, 78(10) is 1001110(2). Since 78(10) is positive, in CP2 we have to add a 0 at the left of the number, so we have 01001110(CP2). But if we had -78(10) we add to apply the additional step to make the CP2 number negative.

So, first of all we get the positive corresponding number, 01001110(CP2), then we invert every number starting from after the first one at the right. -78(10) in CP2 is 10110010.

Converting from CP2 back into base 10 is the same as doing it for binary, with the only difference that is the most left bit is a 1, it gets subtracted instead of added to the others

Binary Coded Decimal (Bcd)

BCD is a simple way to express decimal numbers in binary: every digit of the number is expressed using 4 bits (since the higher digit we can have i 9, and it requires 4 bits).

If we try converting 369 into BCD:

And here’s the code:

Adding a nice menu

Now the only thing missing is a menu to easily use this converter, here is it:

Conclusion

And the converter is now done. I’d like to remind you that the code is available on my github account. In a future I should do a mobile app out of this, to use it on-the-go, since I didn’t have my computer with me when I needed this more than a couple of times.

Hope you like my work 😛