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

后端 未结 7 1620
执笔经年
执笔经年 2021-02-02 15:47

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 decima

7条回答
  •  粉色の甜心
    2021-02-02 16:13

    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
    

提交回复
热议问题