Working with Binary Data in Delphi and C++ Builder


In both Delphi and C++ Builder binary data can be get or set using byte arrays. In recent versions of Delphi and C++ Builder that support TBytes, event parameters and properties that hold binary data are of type TArray<System.Byte>. In older versions of Delphi and C++ Builder which do not already define TBytes, the components explicitly define TBytes as an array of bytes.

Properties and event parameters that may hold binary data have a corresponding string property as well. The binary version has the same name as the string version, but ends with a B. For instance, there is an EOL and an EOLB property in the TCPClient component. Methods that send binary data, such as the SendBytes method of the TCPClient component, accept a byte array directly.

Delphi - Sending Data

To send binary data, pass a variable of type TBytes to a method such as SendBytes.

procedure TForm1.Button2Click(Sender: TObject);
var
myArray: TBytes; // TArray<Byte> may also be used in place of TBytes here.
begin
myArray:= TBytes.Create(72,69,76,76,79);
ipwTCPClient1.ConnectTo('localhost', 777);
ipwTCPClient1.SendBytes(myArray);
end;

Alternatively, a dynamic array may be created and cast to TBytes when passed to the component.

procedure TForm1.Button2Click(Sender: TObject);
var
myArray : array of byte; // dynamic array
begin
SetLength(myArray,5);
myArray[0] := 72;
myArray[1] := 69;
myArray[2] := 76;
myArray[3] := 76;
myArray[4] := 79;
ipwTCPClient1.ConnectTo('localhost', 777);
ipwTCPClient1.SendBytes(TBytes(myArray));
end;

Note that in Delphi versions which do not natively define TBytes (i.e., Delphi 5, 6, 7, 2006) use the second approach of casting a dynamic array to TBytes on assignment.

Delphi - Receiving Data

Two event parameters exist for each value in the API that may contain binary data. For instance, a Text (string) and TextB (TBytes) parameter exist in the DataIn event of the TCPClient component. The event definition explicitly defines both parameters. The parameter of type TBytes may be directly accessed or passed to other methods which accept a TBytes parameter, such as the TEncoding class.

procedure TForm1.ipwTCPClient1DataIn(Sender: TObject; const Text: String;
  const TextB: TBytes; EOL: Boolean);
begin
Memo1.Lines.Add(TEncoding.Default.GetString(TextB));
end;

C++ Builder - Sending Data

To send binary data, pass a variable of type TByteDynArray to a method such as SendBytes.

TByteDynArray myArray;
myArray.set_length(5);
myArray[0] = 72;
myArray[1] = 69;
myArray[2] = 76;
myArray[3] = 76;
myArray[4] = 79;
ipwTCPClient1->SendBytes(myArray);

C++ Builder - Receiving Data

Two event parameters exist for each value in the API that may contain binary data. For instance, a Text (string) and TextB (TByteDynArray) parameter exist in the DataIn event of the TCPClient component. The event definition explicitly defines both parameters. The parameter of type TByteDynArraymay be directly accessed or passed to other methods which accept a TByteDynArrayparameter, such as the TEncoding class.

void __fastcall TForm3::ipwTCPClient1DataIn(TObject *Sender, TipwTCPClientDataInEventParams *e)
{
   Memo1->Lines->Add(TEncoding::Default->GetString(e->TextB));
}

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.