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 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!newsfeed00.sul.t-online.de!t-online.de!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Thu, 01 Feb 2007 23:44:58 +0200 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Reading Float Data from a binary file into ada References: <1170172307.292500.256090@m58g2000cwm.googlegroups.com> <45bf9b8b$0$22527$39db0f71@news.song.fi> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <45c25ddf$0$22513$39db0f71@news.song.fi> Organization: TDC Song Internet Services NNTP-Posting-Host: laku61.adsl.netsonic.fi X-Trace: 1170365919 news.song.fi 22513 81.17.205.61:32975 X-Complaints-To: abuse@song.fi Xref: g2news2.google.com comp.lang.ada:8826 Date: 2007-02-01T23:44:58+02:00 List-Id: Simon Wright wrote: > Niklas Holsti writes: > > >>- Use the functions Shift_Left or Shift_Right together with (I don't know why I wrote Shift_Right; only Shift_Left is useful here.) >> the "or" operator (all from Interfaces) to assemble 4 octets >> (Unsigned_8 values) into one 32-bit value of type >> Interfaces.Unsigned_32. You may have to experiment to >> get the octets in the right order. >> >>- Then use Unchecked_Conversion to go from Unsigned_32 to Float. > > > IF the VB view splits on octet boundaries, it can be easier to read > into an array of 4 octets (32 bits read), do the unchecked conversion > from array-of-4-octets to float with reordering; something like > > v : four_byte_array; > function convert is new ada.unchecked_conversion (four_byte_array, float); > > read (v); > result := convert ((1 => v (4), 2 => v (3), 3 => v (2); 4 => v (1))); > > If not, why not read into unsigned_32 in the first place? Reading into Unsigned_32 would perhaps not give the right octet order for conversion to Float, depending on how the file was written. If the file is written in the right order, it is easiest to just read into Float with no conversions. The 4-octet-array method has two portability drawbacks, I think: - It should be packed, and the compiler might accept that, or not. (I admit that GNAT will accept it on PCs.) - The compiler defines the endianness of the array after packing, so the order in which octets are moved from the file to the array might have to be compiler-dependent. (I admit that this is probably irrelevant for octet-addressed memory.) I agree that a 4-octet array is a good method, and the aggregate syntax is nicer than a nest of Shift_Lefts and "or"s. But I wanted to give the advice without talking about packing and possible compiler-dependent endianness of packed arrays. I think the best method is to use Streams, but that takes some study to get started. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .