The problem is that on a little-endian machine, your 12-bit record component indicated by the bits bbbb bbbb bbbb are not contiguous in memory if read from the file stream by ordinary means. Ada Stream_IO provides a means of dealing with this problem, but it has the added complication that when parts of two or more record components occupy the same byte, the default stream attributes cannot be used, because that byte will be written or read more than once (i.e., once for each component that occupies it). To see why this is true, refer to LRM 95 13.13.2 (9), viz.: For elementary types, the representation in terms of stream elements is implementation defined. For composite types, the Write or Read attribute for each component is called in a canonical order. The canonical order of components is last dimension varying fastest for an array, and positional aggregate order for a record. Bounds are not included in the stream if T is an array type. If T is a discriminated type, discriminants are included only if they have defaults. If T is a tagged type, the tag is not included. Given all of this, I have written a small demonstration program that will write out a file such as you describe. Normally, of course, the types would be declared in their own package, and the stream-oriented attributes would be implemented in the body of that package, but I have combined them all here to keep things to a single file. The source code for the demonstration program is attached. I do not have access to a big-endian machine today, so this program has only been tested on a little-endian machine. Hoe this helps to understand the problem. There have been many questions of similar nature on this newsgroup, so I thought I'd seize upon a real example, and show how I've been solving this kind of problem. If anyone has a better way to do it, I'm all ears. ----- Original Message ----- From: "Karl Ran" Newsgroups: comp.lang.ada To: Sent: June 23, 2001 5:15 AM Subject: Ada records and byte order > 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? > > > Thanks, > Karl > _______________________________________________ > comp.lang.ada mailing list > comp.lang.ada@ada.eu.org > http://ada.eu.org/mailman/listinfo/comp.lang.ada >