このスレッドでのマルコの提案に加えて、ファイルからループで読み取った生のバイトデータ、4096 バイトのバッファーがある状況があります。
次に、そのバッファで既知の 4 バイトの 16 進文字列を検索します。次のコードは問題なくコンパイルできます。バッファーには、シークする 16 進文字列を含むバイトのデータがあることはわかっていますが、IndexDWord は常に false (-1) を返し、その理由がわかりません。「読み取ってから再度読み取る」という奇妙な動作は、2 つの別個のバッファーが必要なためです。1 つは raw バイト データを含み、もう 1 つは char データを含みます。
var
Buffer : array [1..4096] of char;
Buffer2 : array [1..4096] of byte;
const BinaryMarker : DWord = ($54435054); // (instead of Hex, Hex, Hex, etc. It now compiles)
begin
// Code relating to opening the file etc....then
BytesRead := SourceFile.Read(Buffer, SizeOf(Buffer)); // Read a buffer as an array of chars
SourceFile.Position := SourceFile.Position - BytesRead; // Go back to where you just were
BinaryBytesRead := SourceFile.Read(Buffer2, SizeOf(Buffer2)); // And read it again as byte buffer
J := IndexDWord(Buffer2, SizeOf(Buffer2), BinaryMarker);
if J > 0 then // this is never true, even though I know the byte hex pattern exists in the buffer
ShowMessage('Found at offset ' + IntToStr(J));
end;
ヘルプ!FPC 2.7.1 と Lazarus 1.1 を使用しています。IndexDWordのページはこちら