Re: Reading Float Data from a binary file into ada



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
. @ .
.



Relevant Pages

  • Re: Reading Float Data from a binary file into ada
    ... 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). ... There is a C counterpart to this that makes use of a 'union' to grab the data 1 byte 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? ...
    (comp.lang.ada)
  • Re: Reading Float Data from a binary file into ada
    ... 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). ... There is a C counterpart to this that makes use of a 'union' to grab the data 1 byte 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? ... package Unsigned_32_IO is ...
    (comp.lang.ada)
  • Reading Float Data from a binary file into ada
    ... Essentially I was given a Visual Basic program that dumps a binary ... I need to be able to use this data in ada. ... byte at a time, put them into a char array of size 4, then ... use a 32 bit float to reference the data. ...
    (comp.lang.ada)
  • Re: Ada array vs C pointer (call by reference)
    ... I have a C library function which takes a float * as a parameter. ... where data is an array of float which size is determined by pwmCount. ... In Ada, I have: ... I've never heard of an Ada compiler that doesn't, ...
    (comp.lang.ada)
  • Re: Reading Float Data from a binary file into ada
    ... but store it into a 32 bit Float. ... Any reason you can't call this C function from Ada? ... a round about syntax to help me get started in setting up the memory ...
    (comp.lang.ada)

Loading