With this converter you can convert a number from any polyadic number system (base from 2 to 36) to another polyadic number system. What exactly number systems are and how the conversion works, you will learn here.
Table of contents
Short Description
This converter can be used to convert numbers from one number system (with base from 2 to 36) to another polyadic number system.
What exactly are number systems and how are they composed?
Probably everyone has heard about the two expressions dual system or decimal system. But certainly only a few people have really understood what exactly a number system is. Basically, number systems are simply used to represent numbers. At this point we limit ourselves concretely to so-called polyadic number systems. In these number systems, the value of a symbol depends on its position. In principle, polyadic number systems are structured as follows:
Z: Number, ai: Digit, b: Base
In order to understand this better, the principle is illustrated in the following on the basis of the decimal system which we use every day. The decimal system is called that, because the word Deci stands for the value 10. Thus, the above formula looks like this:
an\(a_n\) is the first digit, an-1\(a_{n-1}\) is the second digit and a0\(a_0\) is the last digit before the comma. a-1\(a_{-1}\), however, is the first digit after the decimal point, a-2\(a_{-2}\) is the second etc.
Supporse we have a number with three digits before the decimal point (n = 2\(n=2\)) and two digits after the decimal point (m = 2\(m=2\)), e.g. the number 123,45. Then we have:
In the same way, this can be applied to any number system with a different base, as we will see in the following chapter.
Conversion of the number systems
When converting different number systems into each other, the decimal system plays a key role, since the conversion is always done via this number system.
Conversion to the decimal system
When we want to convert the number of any number system into the decimal system, then in principle we only have to apply the formula already shown before. For example, if we assume that the number 123.45 is not in the decimal system, but in the number system with base 8, then the calculation looks like this:
However, the underlying PHP code calculates this somewhat differently for numerical reasons. Furthermore, the calculation for the integer part and the decimal places is done separately:
// This code must be called twice: once for the part before the comma and once for the part after the comma $result = "0"; $length = strlen(NUMBER); // The number must be a string for ($i = 0; $i < $length; $i++) { $digit = substr(NUMBER, $i, 1); if (!preg_match("#^[0-9]+$#iu", $digit)) { $digit = (string) (ord($digit) - 55); // Convert letter to numeric value } $result = bcadd(bcmul($result, (string) BASIS, 0), $digit, 0); // Horner's method } // The following code is to be applied additionally only if this is the decimal places calculation $result = bcdiv($result, bcpow((string) BASIS, $length, 0), SPECIFIED_PRECISION); $result = explode(".", $result); $result = $result[1];
Conversion from decimal system to other base
To convert a number from the decimal system to a number system with a different base, we must consider the integer part and the decimal places separately. For a more detailed explanation of the following calculation rules, we convert the number 73.42 with base 10 into the number system of base 5.
For the integer part we proceed according to the following method:- Divide the number with rest by the base.
- The rest represents the digit, starting from right to left. The first rest value received is therefore the least significant digit and the last rest value received represents the most significant digit.
- The integer quotient is the starting point for the further calculation. If the quotient = 0, then you are done. Otherwise, jump to step 1 and repeat the whole process.
Specifically for our example, this means:
14 : 5 = 2 -
2 : 5 = 0 -
-
In PHP, this is calculated as follows:
$result = ""; $quotient = NUMBER; do { $rest = (int) bcmod($quotient, (string) BASE); if ($rest > 9) { $rest = chr($rest + 55); // Convert to letter } $result = $rest.$result; $quotient = bcdiv($quotient, (string) BASE, 0); } while ($quotient != "0");
- Multiply the number by the base.
- The number before the decimal point represents the desired digit, starting from left to right.
- The part after the decimal point is the starting point for the further calculation. If this is = 0, then you are done. Otherwise jump to step 1 and repeat the whole process.
In our example, we thus obtain:
0,1 * 5 = 0,5 -
0,5 * 5 = 2,5 -
0,5 * 5 = 2,5 -
-
The PHP code for this is a little bit more extensive, since we must still cover a special case due to the limited calculation accuracy:
$result = ""; $temp = "0.".NUMBER; $max_error = "1"; $i = 0; do { $product = bcmul($temp, (string) BASE, SPECIFIED_PRECISION); $max_error = bcadd($max_error, bcpow((string) BASE, (string) ($i + 1), 0), 0); $explode = explode(".", $product); $integer = $explode[0]; // This if-block covers the special case for a periodic 9, which might have occurred due to the limited precision if (count($explode) == 2) { $number_invalid_digits = strlen($max_error); $number_valid_digits = strlen($explode[1]) - $number_invalid_digits; $valid_digits = substr($explode[1], 0, $number_valid_digits); if (preg_match('#^9+$#iu', $valid_digits)) { $integer = bcadd($explode[0], "1", 0); $product = $integer; } } $temp = bcsub($product, $integer, SPECIFIED_PRECISION); if (((int) $integer) > 9) { $integer = chr($integer + 55); // Convert to letter } $result .= $integer; $i++; } while (!preg_match('#^0.0+$#iu', $temp) && ($i < NUMBER_OF_DECIMAL_PLACES));
Overall, the conversion result is therefore:
Conclusion
We have now learned how exactly polyadic number systems are composed and how we can generally convert different number systems into each other. Furthermore, we have gained a concrete insight into the alogorithm of the converter described here.
TO THE NUMBER SYSTEM CONVERTER