# Conversion to other bases

## How to use this calculator ?

**Used digits**

For bases 2 to 10, we use arabic digits 0123...789.

for bases 10 to 36, we use symbols ABCD...XYZ (uppercase letters) to represent numbers 11,12,13...34,35,36. For these bases (10 to 36), if you input numbers with lower case letters then we automatically convert them to uppercase letters.

For bases 36 to 62, we use lower case letter (abcdef...xyz). For example, base 62 uses digits : 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz that is a total of 62 symbols. Note that for these bases, letter cases are important. For example, A=11 but a=37.

## Numeration systems

**The decimal system**

The decimal system (based on 10), which we commonly use, represents any number by using 10 symbols : 0123456789. In the decimal system, successive powers of 10 are central and used to form numbers,

`10^0, 10^1, 10^2, 10^3... `ie `1, 100, 100, 1000...`

The number 278 can be written as follows,

`278 = 2*10^2 + 7*10^1 + 8*10^0`

Notice that,

the number of units is multiplied by `10^0`,

the number of tens is multiplied by `10^1`,

the number of hundreds is multiplied by `10^2`,

the number of thousands is multiplied by `10^3`and so on.

**The binary system**

The binary system (base-2), widely used in electronics and computers, uses two symbols 0 and 1 to represent any number. Here are some examples,

101 in base 2 = 5 in base 10 (`= 1*2^2 + 0*2^1 + 1*2^0`)

10011 in base 2 = 19 in base 10 (`= 1*2^4 + 0*2^3 + 0*2^2 + 1*2^1 + 1*2^0`)

To convert a number from binary to decimal base, we sum these quantities,

the first digit (from the right) multiplied by `2^0`,

the second digit multiplied by `2^1`,

the third digit multiplied by `2^2`,

the fourth digit multiplied by `2^3` and so on.

**The hexadecimal system**

The hexadecimal system (or base-16), uses 16 symbols 0123456789ABCDEF to represent any number. Note that the symbols ABCDEF represent values from 11 to 15. We could choose abcdef instead or adopt other symbols.

Examples of numbers in hexadecimal base,

125 in base 16 = 293 in base 10 (`= 1*16^2 + 2*16^1 + 5*16^0`)

1fa6 in base 16 = 8102 in base 10 (`= 1*16^3 + 15*16^2 + 10*16^1 + 6*16^0`)

The same scheme applies for other bases. To convert a number from hexadecimal system to decimal system, we have to sum these quantities,

the first digit (from the right) multiplied by `16^0`,

the second digit multiplied by `16^1`,

the third digit multiplied by `16^2`,

the fourth digit multiplied by `16^3` and so on.

## Convert a number from base n to base 10

To illustrate the method, let's convert the number 1206 from base 9 to base 10.

Agreementing to the formula used above (generalized to base 9), the powers of 9 shall be used as follows,

1206 in base 9 = 897 in base 10 (`= 1*9^3 + 2*9^2 + 0*9^1 + 6*9^0`)

Another example: let's convert the number A160B from base 12 to base 10.

The 12-based system uses the following digits: 0123456789AB.

So we convert as follows,

A160B in base 12 = 209963 in base 10 (`= 10*12^4+ 1*12^3 + 6*12^2 + 0*12^1 + 11*12^0`)

## Convert a number from base 10 to base n

Writing a number P in base n is equivalent to finding the coefficients of successive powers of n such as

`P = c_0 * n^0 + c_1 * n^1 + c_2 * n^2 +`... with `c_0, c_1, c_2... `the digits in the base n of P.

Let's illustrate this with an example. How to convert 1205 to 9 base ?

We start by writing the powers of 9. We stop at the power immediately greater than our number 1205: `1 (9^0), 9 (9^1), 81 (9^2), 729 (9^3), 6561 (9^4 > 1205) `

`9^3 < 1205 < 9^4`. So, 1205 will have 4 digits in base 9 and will be written as follows,

`\text {Equation (1):} 1205 = a_3 * 9^3 + a_2 * 9^2 +a_1 * 9^1 + a_0 * 9^0` i.e. 1205 is represented by "`a_3a_2a_1a_0`" in base 9 with,

`a_0, a_1, a_2, a_3` digits from base 9 i.e. numbers from 0.1,... 7 and 8.

Equation (1) can be written in the form,

`1205 = a_3 * 9^3 + r` with r < `9^3` does this remind you of anything ? this is the Euclidean division of 1205 by `9^3` i.e. 729.

By doing the Euclidean division, we get 1205 ÷ 729 = 1 remainder 476

So we have `a_3 = 1` and `r = a_2 * 9^2 + a_1 * 9^1 + a_0 * 9^0 = 476`

Similarly, one can write,

`476 = a_2 * 9^2 + s` with s < `9^2`, similarly, this is the Euclidean division of 476 by `9^2` i.e. 81.

By doing the Euclidean division, we get 476 ÷ 81 = 5 remainder 71

We deduce `a_2 = 5` and `s = a_1 * 9^1 + a_0 * 9^0 = 71`

We reiterate the same process,
71 ÷ 9 = 7 remains 8, we deduce, `a_1` = 7 and `a_0` = 8

It has been proved successively,

`a_3 = 1`

`a_2 = 5`

`a_1 = 7`

`a_0 = 8`

1205 in base 10 is written 1578 in base 9.

To summarize this method,

To convert a number P from base 10 to base n

- Write powers of n (target base)

- Frame P by 2 successive powers of n (by the way, this determines the number of digits of P in base n)

- Perform the Euclidean division of P by the power of n identified in the previous step (lowest value of the two powers)

- The quotient of this division is equal to the first digit of P (starting from the left) in base n

- Repeat successively these divisions by using the remainder as the new dividend and the immediately lower power as divisor until reaching power 1 of n.

## How to Convert a Number from Base 10 to Base 2?

To illustrate the method, we will take the number 77777 as an example.

To convert the number 77777 from base 10 to a number in base 2, we use the method of repeated division by 2, keeping track of the remainders. Here are the detailed steps: 77777 / 2 = 38888 remainder 138888 / 2 = 19444 remainder 0

19444 / 2 = 9722 remainder 0

9722 / 2 = 4861 remainder 0

4861 / 2 = 2430 remainder 1

2430 / 2 = 1215 remainder 0

1215 / 2 = 607 remainder 1

607 / 2 = 303 remainder 1

303 / 2 = 151 remainder 1

151 / 2 = 75 remainder 1

75 / 2 = 37 remainder 1

37 / 2 = 18 remainder 1

18 / 2 = 9 remainder 0

9 / 2 = 4 remainder 1

4 / 2 = 2 remainder 0

2 / 2 = 1 remainder 0

1 / 2 = 0 remainder 1 By reading the remainders in reverse (from the last to the first), we obtain 10010111111010001, which is the representation of 77777 in base 2.

## Convert a number from base n to base m?

The simplest method is to go through the base-10 :

- First convert the number from base n to base-10 by following the first method above

- Then convert the resulting number in base-10 to base m by following the second method above

Example: convert 1526 from base 7 to base 2

1526 in base 7 = 608 in base 10

608 in base 10 = 1001100000 in base 2

We deduce,

1526 in base 7 = 1001100000 in base 2

## Convert a decimal number with fractional part to base n

In order to simplify the issue, we take n = 16 and P = 220.412, P being the decimal number to convert to base 16. The method below can easily be generalized to any base n.

We have,

`P = (220,412)_10`

We try to rewrite P with a precision of 3 decimal places in base 16.

So, we search `c_2, c_1, c_0, c _ (- 1), c _ (- 2), c _ (- 3)`, base 16 digits, such as

`P = (c_2c_1c_0.c_(-1)c_(-2)c_(-3))_16`

Note: the integer part of P in base 16 (part of P before the decimal point) has 3 digits or less because in base 10, the integer part (= 220) has three digits. Hence we search for three digits `c_2, c_1, c_0` for the whole part, not more!

`P = c_2*16^2+c_1*16^1+c_0*16^0+c_(-1)*16^(-1)+c_(-2)*16^(-2)+c_(-3)*16^(-3)`

In order to keep only positive or zero powers of 16, we multiply the two members by `16 ^ 3`, hence,

`16^3*P = c_2*16^5+c_1*16^4+c_0*16^3+c_(-1)*16^2+c_(-2)*16^1+c_(-3)*16^0`

We have therefore reduced the problem to the case explained above, namely, converting the integer number `16^3 * P` (therefore without comma) from base 10 to base 16. See paragraph "Convert a number from base 10 to base n".

By doing successive divisions of `16 ^ 3 * P = 16 ^ 3 * 220.412 = 902808` by the powers of 16, we obtain,

`(16^3*P)_10 = (DC697)_(16)`

Hence,

`P_10 = (DC.697)_(16)`

More formally, we have,

`(16^3*P)_10 = (DC697)_(16)`

`(16^3)_10*P_10 = (DC697)_(16)`

`(1000)_16*P_10 = (DC697)_(16)`

`P_10 = (DC697)_(16) / (1000)_16`

`P_10 = (DC.697)_(16)`

To conclude and generalize, in order to convert a number P with a decimal point, from base 10 to base 16, with a precision of k decimal places, follow these steps,

- Calculate `16^k*P` and keep its integer part

- Convert the latter to base 16 by using the method of successive divisions by the powers of 16 (Cf details of the method above that applies to integer numbers)

- Insert a decimal point after the k-th digit from the right

## Programming

### Python

This python program converts a number from decimal base to another base.

```
import string
digs = string.digits + string.ascii_letters
def int2base (x, base):
if x < 0:
sign = -1
elif x == 0:
return digs [0]
else:
sign = 1
x*= sign
digits = []
while x:
digits.append (digs [int (x% base)])
x = int (x/base)
if sign < 0:
digits.append ('-')
digits.reverse ()
return ''.join(digits)
```