使用 FreePascal(如果没有 FP 示例,则使用 Delphi),给定一个作为“字节数组”的 2048 字节缓冲区,我如何在缓冲区中搜索“StringA”?
var
Buffer : array[1..2048] of byte;
...
repeat
i := 0;
BlockRead(SrcFile, Buffer, SizeOf(Buffer), NumRead);
// Now I want to search the buffer for "StringA"?
...
Thankyou
我认为这将在 fpc 中工作,无需额外的 Unicode/AnsiString 转换:
function Find(const buf : array of byte; const s : AnsiString) : integer;
//returns the 0-based index of the start of the first occurrence of S
//or -1 if there is no occurrence
var
AnsiStr : AnsiString;
begin
SetString(AnsiStr, PAnsiChar(@buf[0]), Length(buf));
Result := Pos(s,AnsiStr) - 1; // fpc has AnsiString overload for Pos()
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)