comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Reading Float Data from a binary file into ada
Date: Wed, 31 Jan 2007 18:59:59 -0800
Date: 2007-01-31T18:59:59-08:00	[thread overview]
Message-ID: <45GdnVwAocsGylzYnZ2dnUVZ_oannZ2d@comcast.com> (raw)
In-Reply-To: 1170268699.548152.214890@q2g2000cwa.googlegroups.com

"frikk" <frikker@gmail.com> wrote in message 
news:1170268699.548152.214890@q2g2000cwa.googlegroups.com...
> On Jan 31, 9:12 am, "frikk" <frik...@gmail.com> wrote:
>> On Jan 30, 10:54 pm, "Steve" <nospam_steve...@comcast.net> wrote:
>>
[snip]
> 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
> 





  parent reply	other threads:[~2007-02-01  2:59 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
2007-01-30 16:37 ` Dmitry A. Kazakov
2007-01-30 16:55 ` Jean-Pierre Rosen
2007-01-30 17:35   ` Ali Bendriss
2007-02-01 16:28   ` Ali Bendriss
2007-02-01 17:27     ` Jean-Pierre Rosen
2007-01-30 17:56 ` Jeffrey R. Carter
2007-01-30 18:04   ` frikk
2007-01-30 19:56     ` frikk
2007-01-31 18:30       ` Jeffrey R. Carter
2007-01-30 20:51     ` Robert A Duff
2007-01-31 18:25     ` Jeffrey R. Carter
2007-01-31 18:57       ` frikk
2007-01-30 19:31 ` Niklas Holsti
2007-01-30 21:14   ` Cesar Rabak
2007-01-30 21:36     ` frikk
2007-02-01 21:11   ` Simon Wright
2007-02-01 21:44     ` Niklas Holsti
2007-02-03 12:13       ` Simon Wright
2007-02-03 14:59         ` Gautier
2007-02-03 17:01           ` Simon Wright
2007-01-30 20:02 ` Martin Dowie
2007-01-30 20:09   ` frikk
2007-01-30 20:23     ` Martin Dowie
2007-01-31  3:54 ` Steve
2007-01-31  8:42   ` Maciej Sobczak
2007-01-31 14:12   ` frikk
2007-01-31 18:38     ` frikk
2007-02-01  0:05       ` Jeffrey R. Carter
2007-02-01  2:59       ` Steve [this message]
2007-02-01 16:05         ` Bob Spooner
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox