Re: Reading Float Data from a binary file into ada
- From: Niklas Holsti <niklas.holsti@xxxxxxxxxxxxx>
- Date: Tue, 30 Jan 2007 21:31:08 +0200
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
. @ .
.
- Follow-Ups:
- Re: Reading Float Data from a binary file into ada
- From: Cesar Rabak
- Re: Reading Float Data from a binary file into ada
- References:
- Reading Float Data from a binary file into ada
- From: frikk
- Reading Float Data from a binary file into ada
- Prev by Date: Re: Ravenscar - program termination
- Next by Date: Re: Wasteful internationalization
- Previous by thread: Re: Reading Float Data from a binary file into ada
- Next by thread: Re: Reading Float Data from a binary file into ada
- Index(es):
Relevant Pages
|
Loading