![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
hex to float |
Posted: Wed Feb 06, 2008 11:24 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 06, 2008 11:37 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Wed Feb 06, 2008 1:51 pm |
|
|
Ttelmah,
i used those function, and it works....
thanks.
regards. _________________ Andre |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|