gpt4 book ai didi

delphi - 如何在流中搜索十六进制?

转载 作者:行者123 更新时间:2023-12-03 15:31:03 27 4
gpt4 key购买 nike

如何高效快速地搜索流中的十六进制?
如果码流很大,如何在码流中快速搜索并重新调整位置?

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,这个函数高效吗?你能给我一个高效的十六进制搜索功能吗?

最佳答案

我已经改编了 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;

关于delphi - 如何在流中搜索十六进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8741449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com