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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ea4b92bb88a43e50 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-23 09:35:29 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!hammer.uoregon.edu!newsfeed.direct.ca!look.ca!newsfeed1.earthlink.net!newsfeed.earthlink.net!newsmaster1.prod.itd.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3B34C543.2EDC59C9@acm.org> From: Jeffrey Carter X-Mailer: Mozilla 4.7 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada records and byte order References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Sat, 23 Jun 2001 16:35:28 GMT NNTP-Posting-Host: 206.133.138.66 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 993314128 206.133.138.66 (Sat, 23 Jun 2001 09:35:28 PDT) NNTP-Posting-Date: Sat, 23 Jun 2001 09:35:28 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net X-Received-Date: Sat, 23 Jun 2001 09:33:20 PDT (newsmaster1.prod.itd.earthlink.net) Xref: archiver1.google.com comp.lang.ada:9059 Date: 2001-06-23T16:35:28+00:00 List-Id: Karl Ran wrote: > > Hi there, > > I've a question about Ada records: > > I have to read a binary file from disk which was written by another program. > > The record size is fixed: 3 bytes > The file structure looks like this: > > > |byte 1 | |byte 2 | |byte 3 | |byte 4... > aaaa.aaaa.bbbb.bbbb.bbbb.cccc | aaaa.a..... > M L M L M L > S S S S S S > B B B B B B > > a: 8 bits > b: 12 bits > c: 4 bits > > Here is the code ... > > procedure test is > > subtype BYTE is Integer range 0 .. 2 ** 8 - 1; > subtype WORD12 is integer range 0 .. 2 ** 12 - 1; > subtype WORD4 is integer range 0 .. 2 ** 4 - 1; > > type My_rec is > record > A : BYTE; > B : WORD12; > C : WORD4; > end record; > > for My_rec use > record > A at 0 range 0 .. 7; > B at 1 range 0 .. 11; > C at 2 range 0 .. 3; > end record; > > Abc : My_Rec; > > begin > Abc.b := 16#123#; > ... > end test; > > ... which fails to compile whith GNAT on a (low-endian) i386: > test.adb:31:10: component "C" overlaps "B" at line 30 > > Is there an Ada like (TM) solution for this kind of (byte order) problem? On a little-endian machine, you are placing B in all of byte 3 and the 4 LSBs of byte 2. Since you are also placing C in the 4 LSBs of byte 3, you are attempting to overlap these components. The basic problem is that B is "at 1 range 4 .. 15" on a little-endian machine. 15 is the MSB, and 0 the LSB, of a 16-bit word, on a little-endian machine. Your numbering would be appropriate on a big-endian machine. You can use System.Default_Bit_Order to obtain endian-independent bit numbers, since it is now required to be static. You might also want to use modular types rather than subtypes of Integer, such as subtype Byte is Interfaces.Unsigned_8; type Word_12 is mod 2 ** 12; type Nibble is mod 2 ** 4; You might also want to include 'Size clauses on your types. The objects (variables) that you use to read these values from the file should be explicitly set to 24 bits to help ensure that you don't read 32 bits and throw away 8. -- Jeff Carter "Perfidious English mouse-dropping hoarders." Monty Python & the Holy Grail