如何搜索字节数组" StringA"? [英] How to search an array of bytes for "StringA"?
本文介绍了如何搜索字节数组" StringA"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用FreePascal的(或Delphi,如果没有计划生育的例子),给予2048字节的缓冲区是作为一个字节数组,我怎么能搜索StringA缓冲区?
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"?
...
三江源
推荐答案
我认为这将在FPC工作,没有额外的Uni code / AnsiString类型转换:
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;
这篇关于如何搜索字节数组" StringA"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文