We will convert number from one base to another base here. There is one way which is very common. We convert the number to base-10 first. Then from base-10 number, we convert it to the new base. Most of us prefer that as base-10 arithmetic is intuitive to us. Here we won’t be discussing that. Instead, we will try to learn direct conversion from one base to another base. For practical examples, we will take two non base-10 number systems. Obviously you can follow the same conversion logic in case one number system is base-10. We have intentionally taken two non base-10 number systems. That way, we can touch upon how arithmetic works in other number systems.
We will also do conversion from higher base to lower base & vice versa. There is a difference between converting from higher base to lower base & converting from lower base to higher base. We will understand it when we do the conversion with actual examples. But the general concept to convert a number from one base to another remains the same:
- Divide the number by the value of other base.
- Get the remainder. That is the least significant digit.
- Divide the quotient with the value of other base.
- Add the remainder as next least significant digit.
We need to keep doing steps 3 & 4 repeatedly till quotient becomes zero.
Now let’s understand the concept with an example. Suppose we have two number systems, one is base-8 & another is base-4.
base-8 numbers: 0 1 2 3 4 5 6 7
base-4 numbers: 0 1 2 3
Converting a number from higher base to lower base (base-8 to base-4):
Input: 76 (base-8)
We will follow the steps we mentioned above:
We need to divide 7 with 4. 4 is the value of the base we are converting to. And 4 (base-4) is equivalent to 4 (base-8), so we are good. Remember, we are doing arithmetic in base-8 here.
Let's build a multiplication table in base-8. 4 * 1 = 4 4 * 2 = 4 + 4 Addition: 4 4 ----- 10 4 * 3 = 10 + 4 Addition: 10 4 ----------- 14 4 * 4 = 14 + 4 Addition: 14 4 --------- 20 4 * 5 = 20 + 4 Addition: 20 4 ------ 24 4 * 6 = 24 + 4 Addition: 24 4 ------------ 30 4 * 7 = 30 + 4 Addition: 30 4 ------ 34 4 * 1 = 4 4 * 2 = 10 4 * 3 = 14 4 * 4 = 20 4 * 5 = 24 4 * 6 = 30 4 * 7 = 34
We will start the actual division now using the multiplication table.
__17__________ 4 | 76 4______ 36 34______ 2
Remainder is 2. So least significant digit in base-4 is 2.
Quotient is 17.
__3__________ 4 | 17 14______ 3
Remainder is 3. So next least significant digit in base-4 is 3. Number in base-4 is now 32.
Quotient is 3.
__0__________ 4 | 3
Remainder is 3. So base-4 number now becomes 332.
Quotient is 0.
As quotient is 0, we are done with conversion. 76 (base-8) is equivalent to 332 (base-4).
Converting number from lower base to higher base (base-4 to base-8):
Input: 3211 (base-4)
We need to divide 3211 with 8 (value of base we are converting to). Here is the catch. We are operating on base-4 number system. 8 is not present in base-4 system. So how do we divide 3211 (base-4) with 8 (base-10)? We need to convert 8 (base-10) to equivalent number in base-4.
Divide the number 8 with 4 (base we are converting to). 8 / 4 = 2 8 % 4 = 0 So least significant digit is 0. Divide quotient 2 from previous step with 4. 2 / 4 = 0 2 % 4 = 2 So next digit is 2. Equivalent base-4 number is 20. Quotient is 0. We don't need any further steps.
So we need to keep on dividing 3211 (base-4) with 20 (base-4) till quotient is 0. One thing we need to always remember is that we are doing arithmetic in base-4 number system.
Let's build a multiplication table. 1 * 20 = 20 2 * 20 = 20 + 20 Addition: 20 20 ------------ 100 3 * 20 = 100 + 20 Addition: 100 20 -------------- 120 20 * 1 = 20 20 * 2 = 100 20 * 3 = 120
Now we will start dividing 3211 (base-4) with 20 (base 4) using the multiplication table above:
__130__________ 20 | 3211 20______ 121 120______ 11
Here is another catch. Remainder is 11 (base-4). We would need to convert it to higher base (base-8) number to get the least significant digit. For that we need another table which represents all base-8 numbers in base-4. We will do the conversion from higher to lower base (base-8 to base-4) as we have done earlier.
7 / 4 = 1, 7 % 4 = 3 1 / 4 = 0, 1 % 4 = 1 7 (base-8) is equivalent to 13 (base-4). 6 / 4 = 1, 6 % 4 = 2 1 / 4 = 0, 1 % 4 = 1 6 (base-8) is equivalent to 12 (base-4). 5 / 4 = 1, 5 % 4 = 1 1 / 4 = 0, 1 % 4 = 1 5 (base-8) is equivalent to 11 (base-4). 4 / 4 = 1, 4 % 4 = 0 1 / 4 = 0, 1 % 4 = 1 4 (base-8) is equivalent to 10 (base-4). Table: base-4 base-8 13 7 12 6 11 5 10 4 3 3 2 2 1 1 0 0
Remainder: 11 (base-4) is 5 (base-8). So least significant digit in base-8 number system is 5.
Quotient: 130 (base-4). We will again divide it with 20 (base-4).
__3______________ 20 | 130 120_____ 10
Remainder: 10 (base-4) is 4 (base-8). So next digit in base-8 system is 4. Base-8 number is now 45.
Quotient: 3. We will again divide it with 20 (base-4).
__0________ 20 | 3
Remainder: 3 (base-4) is 3 (base-8). So new digit in base-8 is 3. So base-8 number becomes 345.
Quotient: 0
The quotient is 0. So we are done with base-4 to base-8 conversion. 3211 (base-4) is converted to 345 (base-8).
As you can see, it is intuitively harder to understand direct conversion for one number base to another number base. That’s why most of us prefer to do the base-10 conversion as an intermediary step. We are accustomed to base-10 number system after all. But understanding direct conversion between non base-10 number systems is helpful in my opinion. It is part of computer science fundamental.