I adapted the code from the POS function available in system.pas :) If you need to find any hex, you can use it like:
PosHex(#$15
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;
source share