Search code examples
delphiarraysdelphi-xe2

Best way to combine multiple TBytes arrays


What is the best way to combine TBytes 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.


Solution

  • 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;