Re: Reading Float Data from a binary file into ada



On Jan 31, 9:12 am, "frikk" <frik...@xxxxxxxxx> wrote:
On Jan 30, 10:54 pm, "Steve" <nospam_steve...@xxxxxxxxxxx> wrote:





"frikk" <frik...@xxxxxxxxx> wrote in message

news:1170172307.292500.256090@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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?

Your description of how the C counterpart works is inconsistant with the way
you have described the data format in the file.

If the C program is using a union to view the data as either a float or a 4
character array then it is very likely that the data is in fact stored in
IEEE 754 floating point format. If you are using Microsoft's C compiler
this is certainly the case.

The following snippet contains the pieces you need to do the conversion.

TYPE aByte IS MOD 256;

FOR aByte'SIZE USE 8;

TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;

PRAGMA PACK( aByteArray );

TYPE aFourBytes IS NEW aByteArray(1..4);

FUNCTION Conv IS NEW Ada.Unchecked_Conversion( aFourBytes, float );

...

a : float;
b : aFourBytes;

...

a := Conv( b )

Use unchecked conversion to convert from a type that contains 4 bytes to the
float value you're looking for.

Regards,
Steve
(The Duck)

Basically I need to be able to read in the binary data byte by byte
but store it into a 32 bit Float. The C union example above uses the
same memory address for the Float as it does for the size 4 char
array. I don't even know if the VB dump will correspond with the way
ada handles floats or not, but I'll worry about that later.

I am also using Matlab/Simulink if that provides any additional tools
to use for debugging.

Thank you for any help,
Blaine- Hide quoted text -

- Show quoted text -

Thank you for the example code! I'll play around with this and see how
it works. Yes, you are correct that the C code and the VB code don't
seem to make sense. I'm going to verify that this C code does work
exactly as we want it to.

Thank you!
Blaine- Hide quoted text -

- Show quoted text -


Alright - so I figured some things out. Don't worry about what I said
about fixed point, I was very confused on how floating point numbers
were stored and it turns out it was stored exactly as it was supposed
to be. I was analyzing the floating point binary like it was a normal
number. Oops... :(

Anyway, I notice that when dumping a binary file and when reading it
in, we are swapping the bytes around. Something like: for (i = 0; i<4;
i++){out[3-i] = in[i];). Is this because of the way the hardware
would interpret the binary data for processing? This would mean that
in memory the binary resembles nothing like the actual ieee format,
even though it works correctly.

So my main question at this point is: How do I create the equivelant
of this:
union float_union
{
float x;
unsigned char c[4];
};

Would the examples given like for this?
Thank you,
Blaine

.



Relevant Pages

  • Re: Accounting changes
    ... Now time values are stored with microsecond precision as float numbers.(I've written code that allows the kernel to write them without any floating point operations.) ... special floating-point format (ie, it is a format that we define, ...
    (freebsd-arch)
  • Re: weird code.
    ... never seen this kind of declaration ever. ... The variable data is a float pointer? ... array of 16384 float objects. ...
    (comp.lang.c)
  • Re: Help a beginner - function with pointer ...
    ... i thought (float *) T would be appealing to ... The result is the value obtained by converting the ... `T' refers to a one-dimensional array whose elements are also ... using a pointer to the array's first element. ...
    (comp.lang.c)
  • Re: Matrix format of data
    ... float navxdot - 4 bytes ... >> I have a binary DGPS file in a PBN format. ... >> but am a basic beginner on using MATLAB so forgive my ignorance. ... >> It is stored as an 8X1 64 byte double array and looks like this in the ...
    (comp.soft-sys.matlab)
  • Re: weird code.
    ... The variable data is a float pointer? ... array of 16384 float objects. ... We just write `sizeof *data' and the compiler (knowing what ...
    (comp.lang.c)