Working with binary data in Delphi and C++ Builder

Delphi and C++ Builder editions use byte arrays (TBytes) to allow operating with binary data.

Date Entered: 09/04/1997    Last Updated: 04/24/2019

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 a DataToSend and DataToSendB property in the IPPort component.

Delphi - Sending Data

To set binary data to a component property assign a variable of type TBytes to the component property. For instance:

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); ipwIPPort1.Connect('localhost',777); ipwIPPort1.DataToSendB := myArray; end;

Alternatively, a dynamic array may be created and cast to TBytes when assigned to the component instance. For instance:

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; ipwIPPort1.Connect('localhost',777); ipwIPPort1.DataToSendB := TBytes(myArray); end;

Note: In Delphi versions which do not natively defined TBytes (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 IPPort 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. For instance:

procedure TForm1.ipwIPPort1DataIn(Sender: TObject; Text: string; TextB: TArray<System.Byte>; EOL: Boolean); begin Memo1.Lines.Add(TEncoding.Default.GetString(TextB)); end;

To send binary data in current versions of Embarcadero you can use the DataToSendB method to send and the DataInTextB property inside the DataIn event to receive data.

C++ Builder sending binary data:

RawByteString test = RawByteString("x\0yz\n\0",6);
ipwIPPort1->DataToSendB = test;

or

RawByteString test = RawByteString("x\0yz\n\0",6);
ipwIPPort1->SetDataToSend(test.c_str(),6);

C++ Builder receiving binary data:
ShowMessage(ipwIPPort1->DataInTextB.Length());
ShowMessage(ipwIPPort1->DataInTextB[4]);
char* data = ipwIPPort1->DataInTextB.c_str();

for (int i=0; i <= ipwIPPort1->DataInTextB.Length(); i++) {
   ShowMessage(IntToStr(data[i]));
}

Delphi sending binary data:
mRBST := 'a' + #0 + 'b' + #0;
IPPort1.DataToSendB := mRBST;

Delphi receiving binary data:
String Data = String(IPDaemon1.DataInTextB);


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