One of the most successful Android apps I published on Google Play is certainly
Base Converter. It converts numbers from
decimal numeral system to
binary,
hexadecimal,
octal and vice versa. Some users have recently asked me an explanation for the algorithms used to perform the conversion, but it is not convenient to integrate an article within an app, so I wrote this paper to satisfy their request. For convenience I will not write all the algorithms used to perform the conversion, however the techniques shown below will make you able to convert between the four bases (you have to use two algorithms if necessary).
Converting floating point bin 1011011.11001 to dec
We start converting the integer part:
10110112=1⋅26+0⋅25+1⋅24+1⋅23+0⋅22+1⋅21+1⋅20
10110112=64+16+8+2+1=9110
Then we convert the fractional part:
0.110012=1⋅2−1+1⋅2−2+0⋅2−3+0⋅2−4+1⋅2−5
0.110012=1⋅121+1⋅122+0⋅123+0⋅124+1⋅125
0.110012=0.5+0.25+0.03125=0.7812510
So we obtain:
1011011.110012=91.7812510
Converting floating point dec 273.241 to bin
As above we start from the integer part. We divide the integer part by two, annotating the remainder of the division. Then we divide the result of the division by two, and we write again the remainder of the division (before the previous one). The procedure stops when the division result is zero.
2732=136,r=1⟶1
1362=68,r=0⟶01
682=34,r=0⟶001
342=17,r=0⟶0001
172=8,r=1⟶10001
82=4,r=0⟶010001
42=2,r=0⟶0010001
22=1,r=0⟶00010001
12=0,r=1⟶100010001
Now we convert the fractional part. In this case the fractional part is multiplied by two, and we annote the integer part of the result. Then we multiply the result of the previous multiplication by two, and we write again the integer part of the result (after the previous one). The algorthm stops when the result of the multiplication is zero, or when you get sufficient significant digits.
0.241⋅2=0.482⟶0.0
0.482⋅2=0.964⟶0.00
0.964⋅2=1.928⟶0.001
0.928⋅2=1.856⟶0.0011
0.856⋅2=1.712⟶0.00111
0.712⋅2=1.424⟶0.001111
0.424⋅2=0.848⟶0.0011110
0.848⋅2=1.696⟶0.00111101
So the final result is:
273.24110=100010001.001111012
Converting floating point dec 273.241 to hex
The procedure is similar to the previous one, we only change the target radix to 16:
27316=17,r=1⟶1
1716=1,r=1⟶11
6816=0,r=1⟶111
About the fractional part we change the radix to 16 again, in addition we convert the result of the multiplication from decimal to hexadecimal (for this conversion you can refer to
this table):
0.241⋅16=3.856⟶0.3
0.856⋅16=13.696⟶0.3D
0.696⋅16=11.136⟶0.3DB
0.136⋅16=2.176⟶0.3DB2
0.176⋅16=2.816⟶0.3DB22
So we obtain:
273.24110=111.3DB2216
Converting floating point dec 273.241 to oct
We use the same algorithm of the two previous sections,
obviously in this case the target radix is 8:
2738=34,r=1⟶1
348=4,r=2⟶21
688=0,r=4⟶421
Now we convert the fractional part:
0.241⋅8=1.928⟶0.1
0.928⋅8=7.424⟶0.17
0.424⋅8=3.392⟶0.173
0.392⋅8=3.136⟶0.1733
0.136⋅8=1.088⟶0.17331
So the result is:
273.24110=421.173318
Converting floating point bin 101001011.1010011011 to hex
This is the number we want to convert:
101001011.1010011011
First of all we split the digits in groups made of four numbers, possibly we add additional zero digits on the left in the integer part, and on the right of the fractional part to fill the first and the last group:
101001011.1010011011
000101001011.101001101100
Then we convert the binary groups to the hexadecimal base (for this conversion you can refer to
this table).
14B.A6C
So the result is:
101001011.10100110112=14B.A6C10
To perform the conversion from hexadecimal to decimal you have to do the same procedure shown above, by reversing the steps.
Converting floating point bin 101001011.1010011011 to oct
101001011.1010011011
We use the same procedure of the previous section, but the groups are now composed of three digits.
101001011.1010011011
101001011.101001101100
513.5154
101001011.1010011011=513.5154
Quick conversion table for binary and hexadecimal numbers
Binary |
Hexadecimal |
0000
|
0
|
0001
|
1
|
0010
|
2
|
0011
|
3
|
0100
|
4
|
0101
|
5
|
0110
|
6
|
0111
|
7
|
1000
|
8
|
1001
|
9
|
1010
|
A
|
1011
|
B
|
1100
|
C
|
1101
|
D
|
1110
|
E
|
1111
|
F
|