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: Tue, 30 Jan 2007 21:31:08 +0200
Date: 2007-01-30T21:31:08+02:00	[thread overview]
Message-ID: <45bf9b8b$0$22527$39db0f71@news.song.fi> (raw)
In-Reply-To: <1170172307.292500.256090@m58g2000cwm.googlegroups.com>

frikk wrote:
> Hello everyone! I am having a problem that I would love some help 
> with.
> 
> Essentially I was given a Visual Basic program that dumps a binary 
> configuration file with all of the variables in a set.  The variables 
> are each 32 bit floats, with the first 16 bits being the integer part 
> and the second 16 bits being a representation of the fraction (I'm not 
> sure if this is stanard - but its just how VB dumps the data). The 
> binary dump is basically a copy of the way VB stores the data in 
> memory.  I need to be able to use this data in ada.  There is a C 
> counterpart to this that makes use of a 'union' to grab the data 1 
> byte (8 bits) at a time, put them into a char array of size 4, then 
> use a 32 bit float to reference the data.  Is there somehow I can do 
> this in ada as well?

If that approach works in C, the same approach should work in Ada, as 
follows:

- Read the file with Ada.Sequential_IO (instantiated for
   Interfaces.Unsigned_8) or with Ada.Streams.Stream_IO.
   If you haven't used Streams before, Sequential_IO may
   be easier to start with.

- Use the functions Shift_Left or Shift_Right together with
   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.

The portability of this approach is the same as for the C code: it 
depends on the byte order in the VB file, the order in which you 
concatenate the 4 octets into a 32-bit value, and the representation of 
Float values. But if it works in C, it should work in Ada, assuming that 
the Ada Float type has the same representation as the C float type, 
which is likely.

Hope this helps...

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2007-01-30 19:31 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 [this message]
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
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