comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <jrcarter@acm.org>
Subject: Re: Reading Float Data from a binary file into ada
Date: Thu, 01 Feb 2007 00:05:23 GMT
Date: 2007-02-01T00:05:23+00:00	[thread overview]
Message-ID: <79awh.1166103$084.323259@attbi_s22> (raw)
In-Reply-To: <1170268699.548152.214890@q2g2000cwa.googlegroups.com>

frikk wrote:
> 
> 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];
> };

That's pretty simple. You put your input bytes into

subtype Raw_Bytes is System.Storage_Elements.Storage_Array (1 .. 4);

Raw : constant Raw_Bytes := Reorder (Get_Bytes (Raw_Bytes'Length) );

(Raw is in the proper order, of course). You define

function To_Float is new Ada.Unchecked_Conversion
    (Source => Raw_Bytes, Target => Interfaces.C.C_Float);

and then

Result : constant Interfaces.C.C_Float := To_Float (Raw);

You probably want that in some application-friendly type, though, so 
you'd really do something like

Result : constant My_Type := My_Type (To_Float (Raw) );

Finally

return Result;

This can end up being one of those functions where everything's done in 
the declarations.

-- 
Jeff Carter
"Alms for an ex-leper!"
Monty Python's Life of Brian
75



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