comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@nospam.please>
Subject: Re: Reading Float Data from a binary file into ada
Date: Thu, 01 Feb 2007 23:44:58 +0200
Date: 2007-02-01T23:44:58+02:00	[thread overview]
Message-ID: <45c25ddf$0$22513$39db0f71@news.song.fi> (raw)
In-Reply-To: <m2d54t4m81.fsf@grendel.local>

Simon Wright wrote:
> Niklas Holsti <niklas.holsti@nospam.please> 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
       .      @       .



  reply	other threads:[~2007-02-01 21:44 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 [this message]
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
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