Best way to combine multiple TBytes arrays
What is the best way to combine TByt开发者_开发百科es
arrays?
All arrays are of the same size. I want the content of Array2
to be added to the end of Array1
, Array3
to the end of Array2
, and so forth.
To merge two TBytes
together, you have to allocate a third TBytes
that is the total length of the two individual TBytes
, then copy the bytes from both into it. For example:
var
arr1, arr2, merged: TBytes;
begin
...
SetLength(merged, Length(arr1) + Length(arr2));
if arr1 <> nil then Move(arr1[0], merged[0], Length(arr1));
if arr2 <> nil then Move(arr2[0], merged[Length(arr1)], Length(arr2));
end;
You can use string-like operations:
var
LArray1, LArray2, LMerged: TBytes;
begin
...
LMerged := LArray1 + LArray2;
end;
Or you can use the system "Concat" function:
var
LArray1, LArray2, LMerged: TBytes;
begin
...
LMerged := Concat(LArray1, LArray2);
end;
精彩评论