Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a decimal to a mixed-radix (base) number

How do you convert a decimal number to mixed radix notation?

I guess that given an input of an array of each of the bases, and the decimal number, it should output an array of the values of each column.

like image 363
nickf Avatar asked Apr 17 '09 06:04

nickf


People also ask

How do you convert decimal to radix?

Converting From Decimal to Another Radix For an example, we'll use 12,573. If we divide it by 10 we get 1,257, with a remainder of 3. The remainder of the division is the least significant digit of the original number. If we divide 1,257 by 10, we get 125 with a remainder of 7 This gave us the next digit.

How would you convert a decimal integer number to any other radix?

To convert a decimal integer number (a decimal number in which any fractional part is ignored) to any other radix, all that is needed is to continually divide the number by its radix, and with each division, write down the remainder. When read from bottom to top, the remainder will be the converted result.

How do you convert a decimal to a base number?

Decimal to Other Base System Step 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.


2 Answers

Pseudocode:

bases = [24, 60, 60]
input = 86462                       #One day, 1 minute, 2 seconds
output = []

for base in reverse(bases)
    output.prepend(input mod base)
    input = input div base          #div is integer division (round down)
like image 163
Artelius Avatar answered Jan 05 '23 02:01

Artelius


Number -> set:

factors = [52,7,24,60,60,1000]
value = 662321
for i in n-1..0
  res[i] = value mod factors[i]
  value = value div factors[i]

And the reverse:

If you have the number like 32(52), 5(7), 7(24), 45(60), 15(60), 500(1000) and you want this converted to decimal:

Take number n, multiply it with the factor of n-1, continue for n-1..n=0

values = [32,5,7,45,15,500]
factors = [52,7,24,60,60,1000]

res = 0;
for i in 0..n-1
  res = res * factors[i] + values[i]

And you have the number.

like image 40
Toon Krijthe Avatar answered Jan 05 '23 01:01

Toon Krijthe