Search code examples
octal

single algorithm to convert octal to other number systems


I have come across a pretty neat algorithm to convert a decimal number to other number systems. program asks for two inputs: number to convert and the base. the output is the number in the base required. I am wondering if there is possible a single algorithm to convert an octal number to the base of choice?


Solution

  • Of course it is possible. Any number in any base can be written in any other base. For example, it is pretty easy to convert base 8 to base 2, just go one by one from the back and write each number in base 2 using length 3 (this works because 8 = 2^3), e.g.

                  0o1234 
        1 > 001
        2 >    010
        3 >       011
        4 >          100
          0b001010011100
    

    I bet the same algorithm you use to convert from base 10 to other bases can be easily modified to base 8.