How do I read integers from big-endian binary file if Windows/Delphi/IDE implies little-endian order?
The problem has nothing to do with endianness, but with Delphi records.
You have
type
TFasMainHeader = record
fFrmt : array[1..4] of ansiChar;
fVersion : Word;
fDir : array[1..4] of ansiChar;
fNumber : array[1..4] of Byte; //
fElType : Word;
fElSize : Word;
fNumEls : array[1..4] of Byte; //
fDataSize : Integer;
fDataOffset : Integer;
fDO : word;
fDataHandle : array[1..98] of Byte;
end;
and you expect this record to overlay the bytes in your file, with fDataSize
"on top of" 00 00 0A 80
.
But the Delphi compiler will add padding between the fields of the record to make them properly aligned. Hence, your fDataSize
will not be at the correct offset.
To fix this, use the packed
keyword:
type
TFasMainHeader = packed record
fFrmt : array[1..4] of ansiChar;
fVersion : Word;
fDir : array[1..4] of ansiChar;
fNumber : array[1..4] of Byte; //
fElType : Word;
fElSize : Word;
fNumEls : array[1..4] of Byte; //
fDataSize : Integer;
fDataOffset : Integer;
fDO : word;
fDataHandle : array[1..98] of Byte;
end;
Then the fields will be at the expected locations.
And then -- of course -- you can use any method you like to swap the byte order.
Perferably the BSWAP
instruction.