From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3da2be2de13d14c7,start X-Google-Attributes: gid103376,public From: nobody@REPLAY.COM (Anonymous) Subject: Regarding: Extracting contents of binary data files Date: 1997/09/29 Message-ID: <199709291639.SAA04812@basement.replay.com>#1/1 X-Deja-AN: 276470903 References: <342eedac.49311445@newsbeta.alt.net> X-001: Replay may or may not approve of the content of this posting X-002: Report misuse of this automated service to X-URL: http://www.replay.com/remailer/ Organization: Replay and Company UnLimited Mail-To-News-Contact: postmaster@nym.alias.net Newsgroups: comp.lang.ada Date: 1997-09-29T00:00:00+00:00 List-Id: 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