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. 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 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; EOL: Boolean); begin Memo1.Lines.Add(TEncoding.Default.GetString(TextB)); end;

C++ Builder - Sending Data

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

TByteDynArray myArray; myArray.set_length(5); myArray[0] = 72; myArray[1] = 69; myArray[2] = 76; myArray[3] = 76; myArray[4] = 79; ipwIPPort1->DataToSendB = 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 IPPort 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. For instance:

void __fastcall TForm3::ipwIPPort1DataIn(TObject *Sender, TipwIPPortDataInEventParams *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 kb@nsoftware.com.