IEEE Floats from a file to ram.
I have a bit of a problem reading in some data that should be a float.
In the file I have this:
"¿ " (Cut from notepad)
In memory I have this:
BF 00 00 00 (From the debugger)
Which more or less looks the same.
The modelling software that generated the data has this:
-0.5
How do I turn that blob or ram in to something I can understand as a normal float. The modelling software file spec says that it writes IEEE floats.
Could some kind soul explain to me how this works?
As a side note does anyone know what the memory footprint of an IEEE float is meant to be?
I have tried blind casting a float to that hunk of ram but it always come back with a value of 191, if I reverse order the bytes I get some gigantic number.
Many thanks
Chris
Chris Brodie
January 12, 2001 10:18 PM
A float is 4 bytes, and your hex value is 4 bytes.
You don''t have to manually convert it into a float. Just
use fread(), referencing the address of a floating point variable,
and it will do the work for you.
FLOAT f;
fread( &f, sizeof(f), 1, File );
If the data was written as a float, then you should be able to
read it back the same way.
If you talking about the Lightwave modeler and .LWO files, then well,
you have to do it yourself because those idiots write their stuff
in reverse.
First you need to reverse the bits to little-endian style, then
cast it to a float.
big-endian = the most significant byte has the lowest address
little-endian = the least significant byte has the lowest address
You don''t have to manually convert it into a float. Just
use fread(), referencing the address of a floating point variable,
and it will do the work for you.
FLOAT f;
fread( &f, sizeof(f), 1, File );
If the data was written as a float, then you should be able to
read it back the same way.
If you talking about the Lightwave modeler and .LWO files, then well,
you have to do it yourself because those idiots write their stuff
in reverse.
First you need to reverse the bits to little-endian style, then
cast it to a float.
big-endian = the most significant byte has the lowest address
little-endian = the least significant byte has the lowest address
Thanks for the reply,
Yes I''m reading in a Lightwave file. The spec lists the float format as being a IEEE 32 bit float in Big Endian. Probably because the Video Toaster was developed on the Amiga which used motorola processors.
Ok, so a float is a float. To reverse the byte ordering I''ve just being reading the bytes backwards, as in 3,2,1,0 (As a matter of interest the whole file is already in an array (a vector actually).)
I initially thought this would be sufficient but when I flip the bytes I get some gigantic number. I know the number is actually -0.5f so this is a little frustrating.
I''m currently reading in the PNTS chunk of a simple cube from the SDK so I know what value it''s meant to have.
Any advice would be kindly accepted.
Chris
Yes I''m reading in a Lightwave file. The spec lists the float format as being a IEEE 32 bit float in Big Endian. Probably because the Video Toaster was developed on the Amiga which used motorola processors.
Ok, so a float is a float. To reverse the byte ordering I''ve just being reading the bytes backwards, as in 3,2,1,0 (As a matter of interest the whole file is already in an array (a vector actually).)
I initially thought this would be sufficient but when I flip the bytes I get some gigantic number. I know the number is actually -0.5f so this is a little frustrating.
I''m currently reading in the PNTS chunk of a simple cube from the SDK so I know what value it''s meant to have.
Any advice would be kindly accepted.
Chris
Chris Brodie
Here is how I''m currently (incorrectly) reading in the floats:
Thanks
/*-----------------------------------------------------------------------------Get a Lightwave F4 data type from the array-----------------------------------------------------------------------------*/const float CLightwaveModel::GetF4(void){ //Advance the index m_ModelFileIndex += sizeof(float); return (((unsigned char) m_ModelFile[m_ModelFileIndex-4]<<24) +((unsigned char) m_ModelFile[m_ModelFileIndex-3]<<16) +((unsigned char) m_ModelFile[m_ModelFileIndex-2]<<8) +(unsigned char) m_ModelFile[m_ModelFileIndex-1]); }
Thanks
Chris Brodie
const float CLightwaveModel::GetF4(void){ //Advance the index m_ModelFileIndex += sizeof(float); int t = (((unsigned char)m_ModelFile[m_ModelFileIndex-4]<<24) +((unsigned char)m_ModelFile[m_ModelFileIndex-3]<<16) +((unsigned char)m_ModelFile[m_ModelFileIndex-2]<< 8) + (unsigned char)m_ModelFile[m_ModelFileIndex-1]); return *(float*)&t}
Thanks heaps... Works like a bought one...
Some questions though so I can understand what going on. You assign int t. The t is just there to store the address right. The int isn''t big enough to hold a float. This step also reverses the order of the bytes memory.
*(float*)&t
Says, treat the address of t like and address of a float. Then the first * says get the data at that resultant address.
Is that right? If thats so then int could have been anything that could store an address?
Thanks again
Chris
Some questions though so I can understand what going on. You assign int t. The t is just there to store the address right. The int isn''t big enough to hold a float. This step also reverses the order of the bytes memory.
*(float*)&t
Says, treat the address of t like and address of a float. Then the first * says get the data at that resultant address.
Is that right? If thats so then int could have been anything that could store an address?
Thanks again
Chris
Chris Brodie
quote: Original post by gimp
You assign int t. The t is just there to store the address right. The int isn''t big enough to hold a float.
"int" in this case is just a memory array, where you can put bytes in the reverse order.
32bit "int" = 4byte, same as 32bit "float".
*(float*)&t // read this 4 reversed bytes as "float"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement