lakdee
lakdee

Reputation: 55

Using Byte[] in delphi

I have a c# project which I am trying to convert into Delphi.

I have a function 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;
}

is there any equivalent to this in Delphi?

Upvotes: 1

Views: 3828

Answers (1)

Wosi
Wosi

Reputation: 45153

The Delphi equivalent of byte[] in C# is array of byte. You could either use it with a fixed size:

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

or with dynamic size

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;

For some basic types there are also predefined array types which should be preferred to use. For example TBytes.

Generics have been introduced in Delphi 2009. If you are using at least this version you should prefer to use TArray<T>. For example TArray<integer> or TArray<TMyType>.

Upvotes: 1

Related Questions