tonix
tonix

Reputation: 6939

What is the algorithm to convert a float of a base to the float of another base?

I would like to ask if someone of you knows how to achieve this:

Let's say I have a float like 0.56 but with the base N and I want to convert it into a float with a base T, how can I achieve it? Is there a formula or something?

For example, if I have to convert 0.56 from base 8 to base 16, I know it is 0.B8 (I do it by hand converting 0.56 (base 8) to base 2 -> 0.101110 and then I group the bits by 4 starting from the float point, so 1011 & 10 (1000) is B and 8, thus B8).

But what if I want e.g. from base 8 convert to base 6 in a programmatic way? Do not need code, just need to understand how this is achieved in a an automatic way.

Thanks for the attention!

Upvotes: 0

Views: 91

Answers (1)

davdan angelo
davdan angelo

Reputation: 31

Their is a way to do this for all integers including negative bases.

Separate it into it's fraction parts.

x=number

b=base

n=digits

Loop i:

(x/(ib))%b=n

n_etc. n4 n3 n2 n1

If you didn't separate it into it's fractional parts then the parts after the decimal are evaluated using the multiplicative inverse of that base.

If you want fractional or irrational bases then I refer you to this, but I have yet to figure out how to get it to work for negative bases.

https://math.stackexchange.com/questions/1938993/converting-bases/1939925#1939925

Upvotes: 2

Related Questions