应用场景
前面的函数是比较字节数组是否完全相同,而我们有时候只需要比较开头几个元素是否相同。
函数代码
function ByteArraysSameStart(const B1, B2: array of Byte; const Count: Integer):
Boolean;
var
I: Integer;
begin
Assert(Count > 0, 'Count must be >= 1');
Result := False;
if (Length(B1) < Count) or (Length(B2) < Count) then
Exit;
for I := 0 to Pred(Count) do
if B1[I] <> B2[I] then
Exit;
Result := True;
end;
测试
procedure TForm1.FormCreate(Sender: TObject);
const
B1: array[0..3] of Byte=($01, $02, $03, $07);
B2: array[0..4] of Byte=($01, $02, $03, $05, $06);
begin
if ByteArraysSameStart(B1, B2, 3) then
ShowMessage('123');
end;
···
该文章介绍了一个函数,用于检查两个字节数组的开头指定数量的元素是否相同。函数名为`ByteArraysSameStart`,它接受两个字节数组和一个计数作为参数,如果数组开头的元素在给定计数内匹配,则返回True。在测试案例中,当比较数组B1和B2的前三个元素时,显示消息123,说明函数能正确判断开头元素的匹配情况。

431

被折叠的 条评论
为什么被折叠?



