应用场景
有时候需要把两个字节数组合并成一个字节数组,一个函数就可以搞定。
函数代码
procedure AppendByteArray(var B1: TBytes; const B2: array of Byte);
var
OldB1Len: Integer;
begin
if Length(B2) = 0 then
Exit;
OldB1Len := Length(B1);
SetLength(B1, OldB1Len + Length(B2));
Move(B2[0], B1[OldB1Len], Length(B2));
end;
//对AppendByteArray重新封装,实现两个单纯的字节数组拼接返回TBytes类型
function ConcatByteArrays(const B1, B2: array of Byte): TBytes;
begin
Result := CloneByteArray(B1);
AppendByteArray(Result, B2);
end;
测试
procedure AppendByteArray(var B1: TBytes; const B2: array of Byte);
var
OldB1Len: Integer;
begin
if Length(B2) = 0 then
Exit;
OldB1Len := Length(B1);
SetLength(B1, OldB1Len + Length(B2));
Move(B2[0], B1[OldB1Len], Length(B2));
end;
function BytesOf(const Val: AnsiString): TBytes;
var
Len: Integer;
begin
Len := Length(Val);
SetLength(Result, Len);
Move(Val[1], Result[0], Len);
end;
function StringOf(const buf:TBytes): AnsiString;
begin
SetLength(Result, Length(buf));
CopyMemory(PAnsiChar(result), @buf[0], Length(buf));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
B1: Tbytes;
const
B2: array[0..2] of Byte=($37, $38, $39);
begin
B1:= BytesOf('123456');
AppendByteArray(B1, B2);
Caption:= StringOf(B1);
end;
···