# Converting fractional hex number into fractional decimal using pic

Discussion in 'Embedded Systems and Microcontrollers' started by RG23, Jun 28, 2011.

Not open for further replies.
1. ### RG23 Thread Starter Active Member

Dec 6, 2010
301
2
I am using pic16f887.

I want to know how to convert fractional hex number into fractional decimal.

If anyone has an idea please let me know

Thanks

2. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,439
1,627
Of course, a hex number by itself is an integer quantity. However, I will assume you have computed some quantity and have the fractional part saved off somewhere by itself.

The PIC knows nothing about fractions so knowing which processor you are using is somewhat meaningless. What you are trying to accomplish would be more meaningful.

Are you trying to present this information on some sort of display device? Typically one outputs the decimal part by converting to an ASCII string, then a period, then converts the fractional part to another string and output that.

In the past I have avoided such by careful choice of the units of stored quantities. For example, say I wish to present a voltage reading in the form "X.XX V". To do so I work out the math so the resultant answer is in the units of 10 millivolts, so 1 means .01 volts, 101 means 1.01 milivolts, and so on.

Then the value I have is converted in one shot to ASCII, and the string then broken up so the period can be inserted, and that string sent to the display. There is a bit of work inspecting the string length to insert the period in the correct place.

3. ### RG23 Thread Starter Active Member

Dec 6, 2010
301
2
For example I have two hex numbers

96h and 07h

I have to divide them and display on LCD

the result in hex is 15.6d
the result in decimal is 21.42

I am presently displaying 21.6d on LCD

I have not figured out how to convert that fractional 0.6d into 0.42

If any idea please let me know

Thanks

4. ### joeyd999 AAC Fanatic!

Jun 6, 2011
2,757
2,936
do this:

96h * 100d / 07h

then display 4 digits.

even better:

((96h * 200d / 07h) + 1) / 2

(takes care of rounding!)

5. ### MrChips Moderator

Oct 2, 2009
12,636
3,454
I believe you are not giving us the full picture or you do not fully understand binary.
The hex numbers 96h and 07h are stored in binary on the computer.
How are you doing a hex division?

Dec 6, 2010
301
2
7. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,439
1,627
OK, so you have 96H = 150D and 7H which is still 7D, and you wish to present the results to 2 decimal places. I mentioned you get to choose the units of your numbers. Pick units such that there is no decimal remainder after the division.

Start by taking the divident (150) and multiply it by 100 to get 15000, and divide that by your divisor to get 2142. Convert that to ASCII and get the string 2142. Now copy that string in pieces into another string so you can insert a decimal point. Your screen then says "21.42"

Do keep track of how big the variable you are holding the result can handle.

If you can't handle big variables, another way is to compute the decimal portion as you do. Then do a modulus division, which gives you the remainder from a division. Multiply THAT remainder by 100, and again divide by 7. It gives you the fractional portion while keeping the magnitude of the intermediate products smaller (by a factor of your divisor, right?)

150 mod 7 = 3

3 * 100 = 300

300 / 7 = 42

BTW, as a general note when doing integer arithmetic if you want a rounded off division you can always add half the divisor to the dividend to get a rounded off quotient. OP may wish to do that with the fractional part.

In c, that would be:

Qrounded = (dividend + divisor>>2) / divisor;

8. ### MrChips Moderator

Oct 2, 2009
12,636
3,454
I am not sure where you are getting the .6h from.

BTW, 15h.6h is the correct answer for 96h/07h.

The result from the division should be 21 and 65394 remainder.
You have to multiply the remainder by 65536 to get the decimal part = 42856

Sep 7, 2009
2,803
594

Apr 20, 2004
15,815
283