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

hex to float

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



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

hex to float
PostPosted: Wed Feb 06, 2008 11:24 am     Reply with quote

Hi people, Im having some problem with data conversion...

I have to convert 4 byte data to a float, but something strange is happening.

for example the following value:

Code:

//FLOAT

0,0328285

0x3D

0x06

0x77

0x39



using the same float value on this code:

Code:

   x = 0.0328285;      

   printf ("\r\nValue: %.7f", x);
   buf[0] = *((int8 *)&x + 0);
   buf[1] = *((int8 *)&x + 1);
   buf[2] = *((int8 *)&x + 2);
   buf[3] = *((int8 *)&x + 3);


   printf ("\r\nhexa: %x %x %x %x", buf[0],  buf[1],  buf[2],  buf[3]);
   *(int8 *)&y = buf[0];
   *((int8 *)&y +1) = buf[1];
   *((int8 *)&y +2) = buf[2];
   *((int8 *)&y +3) = buf[3];


   printf ("\r\nfloat: %.7f", y);



the result is:

Code:

Value: 0.0328285
hexa: 7a 06 77 2d
float: 0.0328285


the original value is a float converted using labview and sent throught UART.

anyone can help ?


regards.
_________________
Andre
Ttelmah
Guest







PostPosted: Wed Feb 06, 2008 11:37 am     Reply with quote

I'd suspect without looking at the values, that this is the classic IEEE versus CCS/Microchip float problem.
The CCS compiler, uses a float format first used in a Microchip application note, which makes it slightly easier on the PIC to handle the data. However the order of the bits in the byte, is _not_ the same as used in the IEEE standard, which is what is normally used in a PC.
If your compiler is reasonably 'recent' (any time in the last couple of years), look at the 'ieeefloat.c' file in the drivers directory. It contains code to convert the numbers in both directions.
If your compiler predates this, I have posted routines to do this here some time ago, and a search should find them.

Best Wishes
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 06, 2008 1:51 pm     Reply with quote

Ttelmah,

i used those function, and it works....

thanks.

regards.
_________________
Andre
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