CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

convert MCHP float value to regular hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
karth



Joined: 10 Sep 2003
Posts: 29

View user's profile Send private message

convert MCHP float value to regular hex
PostPosted: Tue May 16, 2006 9:10 am     Reply with quote

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

Embarassed Question
karth



Joined: 10 Sep 2003
Posts: 29

View user's profile Send private message

PostPosted: Tue May 16, 2006 9:56 am     Reply with quote

i do not understand the bit representation of the float 32 bit
could anybody help me
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 16, 2006 1:22 pm     Reply with quote

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
PostPosted: Wed May 17, 2006 5:21 am     Reply with quote

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

Embarassed Question


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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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