Processing math: 100%

domenica 14 febbraio 2016

Base conversion of unsigned floating point numbers

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=126+025+124+123+022+121+120
10110112=64+16+8+2+1=9110
Then we convert the fractional part:

0.110012=121+122+023+024+125
0.110012=1121+1122+0123+0124+1125
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=11
1362=68,r=001
682=34,r=0001
342=17,r=00001
172=8,r=110001
82=4,r=0010001
42=2,r=00010001
22=1,r=000010001
12=0,r=1100010001
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.2412=0.4820.0
0.4822=0.9640.00
0.9642=1.9280.001
0.9282=1.8560.0011
0.8562=1.7120.00111
0.7122=1.4240.001111
0.4242=0.8480.0011110
0.8482=1.6960.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=11
1716=1,r=111
6816=0,r=1111

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.24116=3.8560.3
0.85616=13.6960.3D
0.69616=11.1360.3DB
0.13616=2.1760.3DB2
0.17616=2.8160.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=11
348=4,r=221

688=0,r=4421
Now we convert the fractional part:

0.2418=1.9280.1
0.9288=7.4240.17
0.4248=3.3920.173
0.3928=3.1360.1733
0.1368=1.0880.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