Recent

Author Topic: Correct way to merge multiple TBytes  (Read 1971 times)

FlameFox

  • Newbie
  • Posts: 3
Correct way to merge multiple TBytes
« on: April 28, 2021, 05:25:43 pm »
What is the correct way to merge multiple TBytes ?

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Correct way to merge multiple TBytes
« Reply #1 on: April 28, 2021, 07:04:32 pm »
Assuming you mean  SysUtils.TBytes, as with any other array: depends on what you want the end result to be. A simple concatenation can be done e.g. with Move():

Code: Pascal  [Select][+][-]
  1. function JoinBytes(const a, b: TBytes): TBytes;
  2. begin
  3.   SetLength(Result, Length(a)+Length(b);
  4.   if Length (Result) > 0 then begin
  5.     if Length(a) > 0 then Move(a[0], Result[0], Length(a));
  6.     if Length(b) > 0 then Move(b[0], Result[Length(a)], Length(b));
  7.   end;
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

FlameFox

  • Newbie
  • Posts: 3
Re: Correct way to merge multiple TBytes
« Reply #2 on: April 28, 2021, 08:52:18 pm »
Thanks!

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Correct way to merge multiple TBytes
« Reply #3 on: April 28, 2021, 10:45:19 pm »
Use Concat:
Code: Pascal  [Select][+][-]
  1. var
  2.   a,b,c,d,Res: TBytes;
  3. ...
  4. begin
  5. ...
  6.   Res := Concat(a,b,c,d);

wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Correct way to merge multiple TBytes
« Reply #4 on: April 28, 2021, 10:54:59 pm »
Or, in fpc trunk, you can even use the '+' operator:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   a, b, c: TBytes;
  10.   i: byte;
  11.  
  12. begin
  13.   a := [1, 2, 3];
  14.   b := [5, 6, 7, 8, 9];
  15.   c := a + b;
  16.   for i in c do
  17.     WriteLn(i);
  18.  
  19.   ReadLn;
  20. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6321
  • Compiler Developer
Re: Correct way to merge multiple TBytes
« Reply #5 on: April 29, 2021, 09:30:05 am »
Or, in fpc trunk, you can even use the '+' operator:

FPC 3.2.0 is enough. ;)
Though in non-Delphi modes one needs to use $Modeswitch ArrayOperators (the Concat, which was added at the same time, does not require that).

FlameFox

  • Newbie
  • Posts: 3
Re: Correct way to merge multiple TBytes
« Reply #6 on: April 30, 2021, 08:12:00 pm »
Thanks to everybody!

 

TinyPortal © 2005-2018