Re: Reading Float Data from a binary file into ada
- From: "Steve" <nospam_steved94@xxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 18:59:59 -0800
"frikk" <frikker@xxxxxxxxx> wrote in message
news:1170268699.548152.214890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jan 31, 9:12 am, "frikk" <frik...@xxxxxxxxx> wrote:[snip]
On Jan 30, 10:54 pm, "Steve" <nospam_steve...@xxxxxxxxxxx> wrote:
Anyway, I notice that when dumping a binary file and when reading it
in, we are swapping the bytes around. Something like: for (i = 0; i<4;
i++){out[3-i] = in[i];). Is this because of the way the hardware
would interpret the binary data for processing? This would mean that
in memory the binary resembles nothing like the actual ieee format,
even though it works correctly.
So my main question at this point is: How do I create the equivelant
of this:
union float_union
{
float x;
unsigned char c[4];
};
It is interesting that the application is "swizzling" the floating point
values when writing them to disk.
Our application does the same thing before sending values across a TCP/IP
socket.
SUBTYPE u_long IS WinSock.u_long;
SUBTYPE s_float IS float;
TYPE aByte IS MOD 256;
FOR aByte'SIZE USE 8;
TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;
PRAGMA PACK( aByteArray );
FUNCTION nltohf( value : u_long ) RETURN s_float IS
TYPE aFourBytes IS NEW aByteArray(1..4);
FUNCTION Conv IS
NEW Ada.Unchecked_Conversion( aFourBytes, s_float );
FUNCTION Conv IS
NEW Ada.Unchecked_Conversion( u_long, aFourBytes );
temp : aFourBytes := Conv( value );
BEGIN
RETURN Conv( aFourBytes'( temp(4), temp(3), temp(2), temp(1) ) );
END nltohf;
Sorry for the cryptic function name it is consistant with the naming
convention of similar C functions.
nltohf is: network long to host float.
Unchecked conversion is the preferred way of copying the memory image from
one value to another that is of a different type. This is the preferred way
to handle what C was doing with a union.
If you really really really want to alias the same memory from two
variables, you can do something like this:
x : aliased float;
c : aFourBytes;
for c'address use x'address;
But don't do it. As a matter of fact I fully expect that someone else
following this thread will complain about me even mentioning it. This
"trick" is very unsafe, and moves away from some of the reasons for using
Ada in the first place.
Regards,
Steve
(The Duck)
Would the examples given like for this?
Thank you,
Blaine
.
- Follow-Ups:
- Re: Reading Float Data from a binary file into ada
- From: Bob Spooner
- Re: Reading Float Data from a binary file into ada
- Prev by Date: Re: Binding or not binding
- Next by Date: Re: Wasteful internationalization
- Previous by thread: Re: Reading Float Data from a binary file into ada
- Next by thread: Re: Reading Float Data from a binary file into ada
- Index(es):
Relevant Pages
|