Peter Norton wrote:
The Chief wrote:
Please send an example of such code.
<code snippet> bmpinfo = record (*BMP header*) b1, b2 : char; size : integer;
... snip ...
This code reads first two bytes ok (magic BM), then skips two byes (?) and from info.size all values are shifted two bytes.
Of course it does. char requires one byte, and no alignment. An integer requires 4 bytes, and must be aligned at a multiple of 4. Thus there are two packing bytes between b2 and size. You should define size as ARRAY[0..3] OF char; and create your own code to convert that into an integer. This also controls endianess in the file, which is independant of the executing system.
Looking at the remainder of the record, you would be better off creating the type
bmpint = ARRAY[0..3] OF char;
and replacing the integer types in bmpinfo by bmpint. Then you can create routines to extract (and maybe store) between bmpint and integer types.
You MIGHT get away with defining bmpinfo as a PACKED record, but that is not portable and could be broken at any time by a compiler revision.