From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2aaba1527862ef22 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 31 Jan 2007 20:59:39 -0600 From: "Steve" Newsgroups: comp.lang.ada References: <1170172307.292500.256090@m58g2000cwm.googlegroups.com> <1170252776.235803.122220@j27g2000cwj.googlegroups.com> <1170268699.548152.214890@q2g2000cwa.googlegroups.com> Subject: Re: Reading Float Data from a binary file into ada Date: Wed, 31 Jan 2007 18:59:59 -0800 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-RFC2646: Format=Flowed; Original Message-ID: <45GdnVwAocsGylzYnZ2dnUVZ_oannZ2d@comcast.com> NNTP-Posting-Host: 24.20.111.245 X-Trace: sv3-gTH7E3bjNc0DieeMUSUHsHYzSumJVbEhi7oYEPo0NsEN8juP+Ehbpxfv04xJADzHdEEv7KqdX6QPj6/!BumPqQB1T30S+zSGaMpng+7U3c8DIkABb639tTrGfFoTcDXoDw3g0yC7URVg9EOwnIA2frBV3piD!HvvxVvoQmxY6MQ== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:8803 Date: 2007-01-31T18:59:59-08:00 List-Id: "frikk" wrote in message news:1170268699.548152.214890@q2g2000cwa.googlegroups.com... > On Jan 31, 9:12 am, "frikk" wrote: >> On Jan 30, 10:54 pm, "Steve" 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 >