Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a number from Base B1 to Base B2 without using any intermediate base

Is there a way Convert a number from Base B1 to Base B2 without using any intermediate base.

Ex:

214 from base 5 to base 16 without converting it first to decimal and then decimal to hexadecimal.

--

Thanks

Alok Kr.

like image 287
Kumar Alok Avatar asked Aug 18 '10 15:08

Kumar Alok


2 Answers

This is just an artifact of the fact that we use a decimal system. Therefore, you want to (in your head) think of the "value" of every number in decimal. So you convert everything back to base 10. If you knew how to do division and multiplication in other bases, it would be easy to convert back and forth without using base 10 as an intermediate. Most people however, don't usually do base 5 division/multiplication, and will convert everything back to base 10.

The algorithm is the same though. Divide by the largest power of the new base you can and then divide the remainder by the smaller power and you'll get the new base.

For instance 0x3B to base 5.

(math is in base 16)

3B / 5^2 = 2 remainder 9

9 / 5 = 1 remainder 4

so 0x3B = 214 base 5

If you know how to do non base 10 division, it's simple. However, there is absolutely no reason to learn that, so it's much easier to convert back to base 10 as an intermediate step.

However, there is an easy way to convert between binary and hexidecimal. Just split the number into groups of 4 binary/1 hexadecimal digits, and convert digit by digit.

1111 0000 1100 0001 
   F    0    9    1
like image 22
demeteloaf Avatar answered Sep 21 '22 13:09

demeteloaf


To convert 214base5 to base 16 without an intermediate base, you "just" have to know how to calculate directly in base 5.

First, you need a table of what the base 16 digits are in base 5 (you need a similar table when converting base 10 to base 16, it's just that that one is easier to keep in your head!). This table is easy to create - just start at 0 and increment each base 5 row until you reach f in base 16.

base 16 | base 5
--------+--------
      0 |  0
      1 |  1
      2 |  2
      3 |  3
      4 |  4
      5 | 10
      6 | 11
      7 | 12
      8 | 13
      9 | 14
      a | 20
      b | 21
      c | 22
      d | 23
      e | 24
      f | 30

Now you just need to repeatedly divide by 16 (which is 31base5). We now recall our primary school days, and use long division (if this seems hard, it's because no-one made you learn your times-tables in base 5!):

Step 1:

   ______
31 ) 214

Step 2:

       3 
   ______
31 ) 214 -
     143  

Step 3:

       3 
   _____
31 ) 214 -
     143  
    ----
      21

So the result of 214base5 divided by 31base5 is 3base5 remainder 21base5.

This means that the least significant digit in base16 is 21base5, which you can find in the table is bbase16. The result of the division is 3base5 - if this was greater than 30base5 then we would divide again - but it's not, so this means the most significant digit is (using the table again) 3base16.

So the answer is 214base5 = 3bbase16.

like image 53
caf Avatar answered Sep 23 '22 13:09

caf