应用场景
有时候遇到字节数组很长,而我们只需要中间的一小部分,所以需要剪切提取。
函数代码
function SliceByteArray(const B: array of Byte; Start, Len: Integer):TBytes;
begin
if Start < 0 then
Start := 0;
if Len < 0 then
Len := 0
else if Start >= Length(B) then
Len := 0
else if Start + Len > Length(B) then
Len := Length(B) - Start;
SetLength(Result, Len);
if Len > 0 then
Move(B[Start], Result[0], Len);
end;
测试
procedure TForm1.Button1Click(Sender: TObject);
var
B1, B2: Tbytes;
begin
B1:= BytesOf('123456');
B2:= SliceByteArray(B1, 2, 3);
Caption:= StringOf(B2);
end;
···
该文章介绍了一个名为SliceByteArray的函数,用于从给定的字节数组中提取指定开始位置和长度的子数组。函数进行了边界检查并确保不会超出原数组范围。在测试案例中,函数成功地从123456的字节数组中截取了345的子数组。

1351

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



