Re: Porting Binary Files
- From: "Severian [MVP]" <severian@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 13 Apr 2005 17:18:35 GMT
On Wed, 13 Apr 2005 08:09:06 -0700, "Maynard"
<Maynard@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>"Severian [MVP]" wrote:
>> On Tue, 12 Apr 2005 12:35:01 -0700, "Maynard"
>> <Maynard@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>> >I would like to be able to read a ported binary file (populated only by
>> >single precision floating point values) from a 32-bit OS that doesn't run
>> >Windows, to a Windows XP PC with a simple FTP. When I read the data from the
>> >file on my PC (using fstream object), I can see that the data appears in the
>> >right locations (I am reading 4 floats...the first and last 4-bytes are the
>> >same, and the middle 2 4-bytes are the same), but the values are incorrect.
>> >Does anybody know what might be going on here? Is this a lost cause?
>>
>> This depends on:
>>
>> 1) The format of floating point numbers on the originating machine.
>> 2) The byte-order of the originating machine.
>>
>> If the originator uses IEEE floating point, it may just be the second
>> issue, which you can correct by swapping the 4 bytes in each float:
>>
>> #include <assert.h>
>>
>> float GetFloat(BYTE *pf) /* pf is pointer to input buffer */
>> {
>> assert(sizeof(long) >= sizeof(float);
>> return (float)((long)pf[3]|(pf[2]<<8)|(pf[1]<<16)|(pf[0]<<24));
>> }
>>
>> If the format is completely different, though, you'll need to find out
>> more about it and synthesize an IEEE floating point number.
>
>I think I follow now (Big Endian vs. Little Endian)...one stupid question
>though...how do I create a pointer to the leading byte in a float?
I wouldn't read the data directly into floats; the reversed bytes may
produce an NaN. I would recommend reading into a byte buffer, then
converting 4 bytes at a time to a float.
--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real
.
- References:
- Porting Binary Files
- From: Maynard
- Re: Porting Binary Files
- From: Severian [MVP]
- Re: Porting Binary Files
- From: Maynard
- Porting Binary Files
- Prev by Date: Re: Porting Binary Files
- Next by Date: Trimming buffer
- Previous by thread: Re: Porting Binary Files
- Next by thread: Re: How to Disable Minimize/Maximize on a console app
- Index(es):
Relevant Pages
|