decimal number to another base

172 Views Asked by At

There is a formula to convert a decimal number into another base. In below example, we use decimal 255 and we divide it by the base, which could be base 16 or base 2 or another. The first question mark is result of division and the second question mark is the remainder:

255 = ? * b + ?

You continue to repeat this process until dividing the decimal number by base returns a 0.

I tried this with multiple bases and it works regardless of what base I use:

16:

255 = 15 * 16 + 15 
15 =   0 * 16 + 15 

2:

255 = 127 * 2 + 1
127 =  63 * 2 + 1
 63 =  31 * 2 + 1
 31 =  15 * 2 + 1
 15 =   7 * 2 + 1
  7 =   3 * 2 + 1
  3 =   1 * 2 + 1
  1 =   0 * 2 + 1 

What makes this formula work for all bases? I know the formula but I don't see why it works for all bases.

1

There are 1 best solutions below

0
On

For example for $4$ digits of base $x$ :

$ax^3+ bx^2+cx+d=x(ax^2+bx+c+d/x)=x ( x(ax+b+C/x)+d/x)=x(x(x(a+b/x)+c/x)+d/x)=x(x(x(x*a/x+b/x)+c/x)+d/x)$

but on next we will stop cause of zero $x*a/x=x*(0+a/x)$

So as you see dividing by $x$ each time you will have digit on remainder $(remainder/x)$

And what you are doing is reversing this.

$x(x(x(x*a/x+b/x)+c/x)+d/x)$ => $x*(m+d/x)= x*m+d$

($m=x(x(x*a/x+b/x)+c/x)$)