在 delphi 中使用 Byte[]

Using Byte[] in delphi

我有一个 c# 项目,我正试图将其转换为 Delphi。

我有一个函数 addcoord

public static byte[] addCoord(Coordinate C, int ID)
{
    //Create an empty array of length 10
    byte[] ret = new byte[10];

    ret[0] = Convert.ToByte(newClass.Add);
    ret[1] = Convert.ToByte(ID);
    ret[2] = BitConverter.GetBytes(C.X)[0];
    //High-Byte of uInt16 X
    ret[3] = BitConverter.GetBytes(C.X)[1];
    //Low-Byte of uInt16 Y
    ret[4] = BitConverter.GetBytes(C.Y)[0];
    //High-Byte of uInt16 Y
    ret[5] = BitConverter.GetBytes(C.Y)[1];
    ret[6] = C.Red;
    ret[7] = C.Green;
    ret[8] = C.Blue;
    ret[9] = C.Master;

    return ret;
}

在Delphi中是否有与此等效的内容?

C# 中 byte[] 的 Delphi 等价于 array of byte。 您可以使用固定尺寸的它:

var
  buffer = array[0..9] of byte;

或动态大小

type
  TByteArray = array of byte;

function AddCoord(const C: TCoordinate; ID: integer): TByteArray;
begin
  SetLength(Result, 10); 

  Result[0] := C.Red; 
  Result[1] := C.Green; 
end;

对于一些基本类型,还有预定义的数组类型,应该优先使用。例如 TBytes.

泛型已在 Delphi 2009 年引入。如果您至少使用此版本,则应该更喜欢使用 TArray<T>。例如 TArray<integer>TArray<TMyType>.