DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

AppendByteArray

Appends array of bytes B2 to the end of byte array B1.

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;

ByteArraysEqual

Checks if two byte arrays are equal. The arrays are equal if they have the same number of elements and elements at the same position in the array are equal.

function ByteArraysEqual(const B1, B2: array of Byte): Boolean;
var
  I: Integer;
begin
  Result := Length(B1) = Length(B2);
  if Result then
  begin
    for I := 0 to High(B1) do
    begin
      if B1[I] <> B2[I] then
      begin
        Result := False;
        Exit;
      end;
    end;
  end;
end;

ChopByteArray

Deletes a sequence of bytes from byte array B starting at index Start with length Len. If either Start or Len are less than 0 they are taken as zero. If Start is beyond the end of the array or if Len is 0 then the whole array is returned unchanged. If the sequence of bytes to be chopped extends beyond the end of the array it is truncated from Start.

function ChopByteArray(const B: array of Byte; Start, Len: Integer):
  TBytes;
var
  First, Last: 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;
  First := SliceByteArray(B, 0, Start);
  Last := SliceByteArray(B, Start + Len, Length(B));
  Result := ConcatByteArrays(First, Last);
end;

CloneByteArray

Makes a copy of an array of bytes.

function CloneByteArray(const B: array of Byte): TBytes;
begin
  SetLength(Result, Length(B));
  if Length(B) > 0 then
    Move(B[0], Result[0], Length(B));
end;

ConcatByteArrays

Concatenates two byte arrays B1 and B2 and returns the resulting array. The result is the contents of B1 followed by the contents of B2.

function ConcatByteArrays(const B1, B2: array of Byte): TBytes;
begin
  Result := CloneByteArray(B1);
  AppendByteArray(Result, B2);
end;

SliceByteArray

Slices a range of bytes from byte array B, starting at index Start with length Len, and returns the result. If either Start or Len are less than 0, they are taken as 0. If Start is beyond the end of the array or if Len is 0 then an empty array is returned. If the sequence of bytes to be sliced extends beyond the end of the array it is truncated from Start.

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;

View the whole database.

Go to the DelphiDabbler website.