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

后端 未结 7 1633
执笔经年
执笔经年 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:11

    I don't believe there is any "syntactical trick" that allows you to do this for a general base conversion. (A trick that for example allows you to go from the string "214" to the string "3B" without figuring out which integer "214" (base 5) actually corresponds to.)

    By that I mean that you necessarily have to know the value of the number you will work with, that is, you need to "parse" the input.

    214 in base 5 for instance, would be parsed as 2*52 + 1*5 + 4. By doing such computation you won't get it in decimal form. You'll get it in what ever form your computer decides to store the resulting integer in (probably binary :)

    From that point you can easily output the number in, say base 16. (Note that you have not gone via base 10.) As @Lou Franco put it, you've merely gone from string->int->string, instead of string->string.

提交回复
热议问题