comp.lang.ada
 help / color / mirror / Atom feed
From: nobody@REPLAY.COM (Anonymous)
Subject: Regarding: Extracting contents of binary data files
Date: 1997/09/29
Date: 1997-09-29T00:00:00+00:00	[thread overview]
Message-ID: <199709291639.SAA04812@basement.replay.com> (raw)
In-Reply-To: 342eedac.49311445@newsbeta.alt.net


On Sun, 28 Sep 1997 05:45:30 GMT, andy.cornell.nospam@ibm.net wrote:

> Hi:
> 
> I need to extract the contents of some packed binary files (WMO GRIB
> files, to be exact). The data items are contained in a (big-endian)
> continuous stream of bits (no unused bits between data items). The
> number of bits per item doesn't vary within a file, but varies from
> file to file, depending on precision required. The data items are
> unsigned integers, which get converted back to the original real
> values using reference values contained in the header portion of the
> file. The start of the data stream is byte-aligned (I think that's the
> correct terminology), but obviously the following data item's
> alignment with byte boundaries varies.
> 
> I have a Fortran routine which unpacks these files; it reads the
> entire file into an integer array, then performs bit-shift operations
> (IAND, IOR, ISHFT) on bytes to extract and byte-align the data.
> 
> My questions are:
> 1) Is this the best approach using Ada?
> 2) If so, how do I do the bit-shifting in Ada?
> 3) Are there existing Ada library routines available to do this?
> 
> I am using GNAT 3.09 OS/2 (I'll upgrade soon). I am a novice
> programmer (one year of university CS 4 years ago) who is new to Ada;
> I discovered it while reading McConnell's "Code Complete" from
> Microsoft Press. I like what I've seen of Ada so far.
> 
> Thanks in advance.
> 
> Andy Cornell
> andy dot cornell at ibm dot net
> 
> P.S. I'd like to stick with portable solutions as I eventually plan to
> get a big-endian UN*X box.
> 
> 

You have some options. If you're happy with the FORTRAN routine, why not
keep it and call it from Ada? GNAT provides package Interfaces.Fortran
for this purpose.

If you want to write it in Ada, modular types provide for bitwise
logical operations. The modular types defined in package Interfaces also
have shift operations defined for them. So you could read the stream a
byte at a time and use these operations to extract the bit-packed data.

Finally, you might be able to use representation clauses to let you read
all the data with a single operation, and have the compiler figure out
how to extract the individual items. GNAT is pretty good about letting
you have components in records that are not aligned on byte boundaries.

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"Illegitimate-faced bugger-folk!"
Monty Python & the Holy Grail
-- 
Jeff Carter  PGP:1024/440FBE21
"Illegitimate-faced bugger-folk!"
Monty Python & the Holy Grail















      parent reply	other threads:[~1997-09-29  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <342eedac.49311445@newsbeta.alt.net>
1997-09-28  0:00 ` Extracting contents of binary data files Steve Doiel
1997-09-29  0:00 ` Anonymous [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox