This site works best with JavaScript enabled!

Description: Convert Number Systems

Number system converter

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.


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=a_n\cdot b^n+a_{n-1}\cdot b^{n-1}+...+a_1\cdot b^1+a_0\cdot b^0+a_{-1}\cdot b^{-1}+a_{-2}\cdot b^{-2}+...+a_{-m}\cdot b^{-m}$$ \(Z\text{: Number, }a_i\text{: Digit, }b\text{: Base}\)
Z = an * bn + an-1 * bn-1 + ... + a1 * b1 + a0 * b0 + a-1 * b-1 + a-2 * b-2 + ... + a-m * b-m
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:

$$Z=a_n\cdot 10^n+a_{n-1}\cdot 10^{n-1}+...+a_1\cdot 10^1+a_0\cdot 10^0+a_{-1}\cdot 10^{-1}+a_{-2}\cdot 10^{-2}+...+a_{-m}\cdot 10^{-m}$$
Z = an * 10n + an-1 * 10n-1 + ... + a1 * 101 + a0 * 100 + a-1 * 10-1 + a-2 * 10-2 + ... + a-m * 10-m

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:

$$\text{123,45}_{10}=\text{ }1\cdot 10^{2}+2\cdot 10^1+3\cdot 10^0+4\cdot 10^{-1}+5\cdot 10^{-2}$$
123,4510 = 1 * 102 + 2 * 101 + 3 * 100 + 4 * 10-1 + 5 * 10-2

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:

$$\text{123.45}_{8} = 1\cdot 8^{2}+2\cdot 8^1+3\cdot 8^0+4\cdot 8^{-1}+5\cdot 8^{-2}\approx\text{ 83.58}_{10}$$
123.458 = 1 * 82 + 2 * 81 + 3 * 80 + 4 * 8-1 + 5 * 8-2 = 83.5810

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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:
  1. Divide the number with rest by the base.
  2. 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.
  3. 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:

$$ \begin{equation*} \begin{split} 73\text{ : }&5=14&\text{ }\Rightarrow\text{ Rest: }3\\ 14\text{ : }&5=2&\text{ }\Rightarrow\text{ Rest: }4\\ 2\text{ : }&5=0&\text{ }\Rightarrow\text{ Rest: }2\\ \end{split} \end{equation*} $$ $$\Rightarrow\text{ Result: }243$$
73 : 5 = 14 -
14 : 5 = 2 -
2 : 5 = 0 -

-

In PHP, this is calculated as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$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");
For the decimal places, however, we apply the following method:
  1. Multiply the number by the base.
  2. The number before the decimal point represents the desired digit, starting from left to right.
  3. 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:

$$ \begin{equation*} \begin{split} \text{0,42 }\cdot\text{ }&5=\text{2,1}&\text{ }\Rightarrow\text{ }2\\ \text{0,1 }\cdot\text{ }&5=\text{0,5}&\text{ }\Rightarrow\text{ }0\\ \text{0,5 }\cdot\text{ }&5=\text{2,5}&\text{ }\Rightarrow\text{ }2\\ \text{0,5 }\cdot\text{ }&5=\text{2,5}&\text{ }\Rightarrow\text{ }2 \end{split} \end{equation*} $$ $$\Rightarrow\text{ Result: 0,20222222...}$$
0,42 * 5 = 2,1 -
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$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:

$$\text{73,42}_{10}=\text{243,20222...}_5$$
73,4210 = 243,20222...5

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
Share this page
Posted on 03.06.2021 | Last modified on 04.06.2021