如何有效,快速地搜索流中的十六进制? 如果流量很大,如何在流中快速搜索并重新调整位置?
function FindInMemStream(Stream: TMemoryStream; What: String):Integer;
var
bufBuffer, bufBuffer2: array[0..254] of Char;
i: Integer;
begin
Result := 0;
i := 0;
FillChar(bufBuffer, 255, #0);
FillChar(bufBuffer2, 255, #0);
StrPCopy(@bufBuffer2, What);
Stream.Position:=0;
while Stream.Position <> Stream.Size do
begin
Stream.Read(bufBuffer[0],Length(What));
if CompareMem(@bufBuffer,@bufBuffer2,Length(What)) then
begin
Result := Stream.Position-Length(What);
Exit;
end;
i := i + 1;
Stream.Seek(i,0)
end;
end;
我想将函数更改为serch hex,这个函数是否有效?你能给我一个高效的函数来交换十六进制吗?
答案 0 :(得分:3)
我已经调整了system.pas中可用的POS功能代码:) 如果你需要找到一些十六进制,你可以使用如下:
PosHex(#$15#$AA#$04, MyStream);
function PosHex(const SubStr: AnsiString; const StrStream: TMemoryStream): Integer;
var
SubLen, SrcLen, Len, I, J: Integer;
C1: AnsiChar;
Str: PAnsiChar;
begin
SrcLen := StrStream.Size;
SubLen := Length(SubStr);
Result := 0;
if (SubLen <= 0) or (SrcLen <= 0) or (SrcLen < SubLen) then Exit;
StrStream.Position := 0;
Str := StrStream.Memory;
Len := SrcLen - SubLen + 1;
C1 := SubStr[1];
for I := 1 to Len do
begin
if Str[I] = C1 then
begin
Result := I;
for J := 1 to SubLen-1 do
begin
if Str[I+J] <> SubStr[1+J] then
begin
Result := 0;
break;
end;
end;
if Result <> 0 then Exit;
end;
end;
end;
Usage:
procedure TForm3.FormCreate(Sender: TObject);
Var
M: TMemoryStream;
S: AnsiString;
begin
S := 'I would like to Find This string!';
M := TMemoryStream.Create;
M.WriteBuffer(S[1], Length(S));
Memo1.Lines.Add(IntToStr(PosHex('Find This', M)));
M.Free;
end;