|
|
View previous topic :: View next topic |
Author |
Message |
karth
Joined: 10 Sep 2003 Posts: 29
|
convert MCHP float value to regular hex |
Posted: Tue May 16, 2006 9:10 am |
|
|
hey programmers,
I need to know how to convert MCHP float to an hex representation.
example:
1)
float = 2.3
MCHP float representation in HEX = 0x 80 13 64 5A
need to convert = 0x0023
2)
float = 13.5
MCHP float representation in HEX = 801CCCCD
need to convert to = 0x0135
3)
float = 12.0
MCHP float representation in HEX = 82400000
need to convert to 0x0120
Thank you
|
|
|
karth
Joined: 10 Sep 2003 Posts: 29
|
|
Posted: Tue May 16, 2006 9:56 am |
|
|
i do not understand the bit representation of the float 32 bit
could anybody help me |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 16, 2006 1:22 pm |
|
|
Look in the CCS FAQ, in the Compiler Behavior section. Look at this item:
"What is the format of floating point numbers ?"
http://www.ccsinfo.com/faq.php
Also go here:
http://www.piclist.com/techref/microchip/math/fpconvert.htm
Download floatconv10.zip
Open it with Winzip. Drag floatconv.exe onto your desktop.
Run it. In the Format section, select Microchip 32-bit.
Then you can type in a number and convert it to hex bytes or a float. |
|
|
Ttelmah Guest
|
Re: convert MCHP float value to regular hex |
Posted: Wed May 17, 2006 5:21 am |
|
|
karth wrote: | hey programmers,
I need to know how to convert MCHP float to an hex representation.
example:
1)
float = 2.3
MCHP float representation in HEX = 0x 80 13 64 5A
need to convert = 0x0023
2)
float = 13.5
MCHP float representation in HEX = 801CCCCD
need to convert to = 0x0135
3)
float = 12.0
MCHP float representation in HEX = 82400000
need to convert to 0x0120
Thank you
|
Can I suggest that you don't use float...
The format you are outputting, appears to be what is called 'BCD' (binary coded decimal), with each hex digit, representing a single digit in the decimal result. It is different from the normal BCD format, in being coded in 'tenths', instead of in units.
BCD conversion routines have been posted here many times in the past, and these can simply be used by multiplying the value *10, and storing the result as an integer.
However given that you are dealing with a value that can only be in 'tenths', it is worth asking whether 'float' can be avoided entirely. Integer arithmetic, is vastly faster, and more 'accurate' than FP. The problem is that cumulative errors can appear in floating point sums, which don't exist if the same number is handled in integer. This is why some languages have a 'currency' data type, which uses a scaled integer, instead of a FP value, since when working with money, the rounding/scaling errors associated with FP maths are unacceptable.
If you are not performing any arithmetic that basically requires FP (sin/cos/log etc.), then it might be worth considering working with integer types instead.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|