Search code examples
arraysdelphipointersdelphi-5

How to convert array of Byte to PByte in Delphi?


I'm using Delphi 5. I define an array of byte, as follows:

Buffer: Array of BYTE;

How to convert it to a PByte pointer?


Solution

  • A dynamic array is implemented as a pointer, so you can simply type-cast it as-is:

    var
      Buffer: array of Byte;
      P: PByte;
    begin
      SetLength(Buffer, ...);
      P := PByte(Buffer);
      ...
    end;
    

    If you don't want to rely on this implementation detail, you can instead take the memory address of the 1st byte in the array:

    P := @Buffer[0];