How to search an array of bytes for "StringA"?

arrays, byte, delphi, freepascal

Solution

I think this will work in fpc without extra Unicode/AnsiString conversion :

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;

Problem

Using FreePascal (or Delphi if no FP examples), given a 2048 byte buffer that is as an "array of bytes", how can I search the buffer for "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

Original source