How to convert a number from binary to quinary system without using decimal system ? It is possible ? I want to write a program who does it.
-
Is your program supposed to be a step-by-step emulation of the procedure you might follow to convert directly from binary to quinary by hand, or is it sufficient for it to print out the correct string of quinary digits? How many digits might be in the binary number you take as input? – David K Mar 12 '15 at 13:08
-
There is nothing special about the decimal system. The algorithm to convert a number from base $a$ to base $10$ is the same as the algorithm to convert from base $10$ to base $b$ is the same as the algorithm to convert directly from base $a$ to base $b$. Only the parameters are different. – Solomon Slow Mar 12 '15 at 13:33
3 Answers
In scanning a binary number (positive integer) from left to right, a $0$ bit doubles the previous value and a $1$ bit doubles the previous value and adds $1$.
So in left to right scanning, say $1011_2$, values are one ($1$), two ($10$), five ($101$), eleven ($1011)$. The value of this binary numeral is eleven. This idea of doubling or doubling and adding one can be done in any base.
For instance $10111_2$; converting to base $5$ (quinary) would go $1, 2, 10, 21, 43$. Answer $43_5$.
Or $1000101_2$ to base $5$ would go $1, 2, 4, 13, 32, 114, 234$ Answer: $234_5$.
- 40,402
-
Very nice! I'm trying to understand the base 5 examples, for 10111 you wrote 1, 2 (double of 1), 10 (quintuple of 2), 21 (double of 10 + 1), 43 (double of 21 + 1), but for 1000101 you wrote 1, 2 (double of 1), 4 (double of 2), 13 (triple of 4 + 1), 32 (?), 114 (?), 234 (?). Anyway, has this method a name? – sound wave Aug 28 '19 at 22:06
See Donald E. Knuth: The Art of Computer Programming, Volume 2: Seminumerical Algorithms, third edition , chapter 4.4: Radix Conversion.
Knuth discusses four methods of conversion from base $B$ to $b$ using either $B$ or $b$ arithmetic.
Method 1a: Division by $B$ using radix-$b$ arithmetic
Method 1b: Multiplication by $b$ using radix-$B$ arithmetic
Method 2a: Multiplication by $B$ using radix-$b$ arithmetic
Method 2b: Division by $b$ using radix-$B$ arithmetic
Here neither $b$ nor $B$ must be $10$.
Note: You have probably access to arithmetic routines using base $b = 2$ (so you could avoid $b = 10$ as work arithmetic) and then radix convert to $B = 5$.
- 34,562
This question has already been treated in Computer Science Stackexchange.
-
The stated intention of the CS.SE answer is to convert a number from one base to a "pure" number which is then converted to the other base. The practical problem with this is that any computation must actually store some representation of the intermediate result. In this case, within the computer the intermediate result is stored in base two. – David K Mar 12 '15 at 13:05