根据Marco在this thread中的建议,我有一个情况,我有一个原始字节数据缓冲区,4096字节,从文件循环读取。
然后我想在该缓冲区中搜索已知的4字节十六进制字符串。我有以下编译好的代码,我知道缓冲区中有数据字节,包括我寻找的十六进制字符串,但IndexDWord总是返回false(-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的页面是here