使用 Delphi 将数组或内存块的内容复制到另一个特定类型

Copy the contents of array or memory block to another specific type, using Delphi

我需要将数组的内容复制到另一种类型的变量中,如下例所示,但我找不到解决方案。 注意:我需要这个例程来处理 Android 和 Delphi 10.

有人知道怎么做吗?

type TMyType = packed record
  Var_1: Byte;
  Var_2: Byte;
  Var_N: Byte;
end;

Var aa: TMyType;
    vArr: TByte;
begin

  vArr := [1, 2, 3];

  // Copy vArr content to "aa" like this
  aa :=  @vArr;

  // aa.Var_1 = 1;
  // aa.Var_2 = 2;
  // aa.Var_N = 3;

Vitória 的解决方案是我使用的解决方案:

Move(vArr[0], aa, SizeOf(aa));

更正后的代码如下所示:

type TMyType = record
  Var_1: Byte;
  Var_2: Byte;
  Var_N: Byte;
end;

Var aa: TMyType;
    vArr: array of Byte;
begin
   aa.Var_1 := 0;
   aa.Var_2 := 0;
   aa.Var_N := 0;

   vArr := [3, 4, 5];

   // Where "vArr[0]" is the pointer to the first memory location to be copied.
   // SizeOf must to be used for records/data structures and Length for arrays.
   Move(vArr[0], aa, SizeOf(aa));

// Result:
//   aa.Var_1 = 3
//   aa.Var_2 = 4
//   aa.Var_N = 5