Others view: Confinter ♦ Welcome ♦ Ring counter ♦ Disjoint squares ♦ Complex number ♦ ReverseComplement ♦

**Number base conversion** is a method to convert the number from one numeric system (radix) into another (for example, from binary into decimal).

Conversion between the whole numbers is often parts of the standard libraries. For instance, in java, *Integer.toString(int value, int base)* accepts the base as the second parameter, with *Character.MAX_RADIX* holding the maximal radix value this function would handle. However it is also possible to convert the decimal part as well.

When there is no decimal part, the mathematical value *v* of the number, written using the system of the base *K* can found using formula^{[1]}

where is the value of the i-th digit counting from right to left and starting from 0 and the n is the total number of digits to convert (assuming no decimal part). For instance, to convert 123 into the usual decimal system, we get

The same number in the octal system would be

.

Interestingly, the first member in a sequence it is always one () as it does not depend on the value of the K.

Now, what if we extend this formula to allow the negative values of the i? For instance, 123.456 in decimal would be

.

But then - eureka! - the generic formula works for the decimal part as well and can be written as

where now m is the number of digits *after the decimal comma* to convert. Does this work with say binary system? Convert 1.5 = 1.1 in binary:

There are some interesting discussions if it is theoretically possible to implement number base conversion as a stream operation, starting to print the first digits of the result before the conversion is finished and do not storing all digits of the converted value^{[2]}. This may only be useful when converting between extremely large numbers.

The radix *K* need not be an integer. It can also be an arbitrary floating point number, even irrational one. For instance, in the Golder Ratio Base^{[3]} system . Theoretically it can also be a complex number.

Algorithms to find digits from the value mostly rely on the arithmetic *mod* (% in C and Java) operation that produces the *remainder*. It allows to get immediately the first digit of the integer value for any given integer base. For instance, for the same value 123, 123 % 10 = 123 - 12*10 = 3 for decimal system or 123 % 2 = 123 - 2*61 = 1 for binary system.

To find more digits, the value must be divided by the radix, rounding the result down. The *mod* operation between the obtained integer and radix gives us the second digit (counting from the right to left). For instance, the following steps returns us the second digit of 123 in the decimal system (*K*=10):

- 123 / K =
**12.3** - floor(12.3) = 12
- 12 % K = 2

here we highlight in bold the value that will be needed in the next iteration to get remaining digits:

- 12.3 / K =
**1.23** - floor(1.23) = 1
- 1 % K = 1.

Applying these steps further we will see that all remaining digits to the left will be zero: surely, 0123 = 00123 and so on. Developers most often need to work with integer numbers, and the *mod* operation for floats is frequently even not supported. Anyway, we can also logically extend the algorithm towards the fractional part, this time *multiplying* by K rather than dividing. For instance, the steps to find the first decimal digit of 123.456 would be

- 123.456 * K = 1234.56
- floor(1234.56) =
**1234** - 1234 % 10 = 4.

Seems working. And the next digit

- 1234.56 * K = 12345.6
- floor(12345.6) = 12345
- 12345 % K = 5

Indeed seems working.

One of the typical applications of these algorithms (other than writing system libraries to deal with usually built-in conversions between decimal and binary representations) is to write code for expressing time or geographical coordinate values in degrees, minutes and seconds. Very similar code is also needed to support various currency systems.

^{1 }www.cs.umd.edu How to Convert From Base 10 to Base K^{2 }Adam Rosenfield (2009). Number base conversion as a stream operation^{3 }Bergman G (1957). A number system within an irrational base