Passing Binary Data Between Chilkat and Delphi
This example shows how to pass binary data between Chilkat and Delphi.
procedure TForm2.Button1Click(Sender: TObject);
var
bd: HCkBinData;
ByteArray: TBytes;
ByteArray2 :TBytes;
ByteArray3 :TBytes;
DataPtr: Pointer;
DataLength: Integer;
begin
// Create an instance of a Chilkat BinData object
bd := CkBinData_Create();
// Create a byte array in Delphi
ByteArray := TBytes.Create(0, 1, 2, 3, 4, 5, 6, 7, 8);
// Append the Delphi bytes to the BinData.
CkBinData_AppendData(bd,@ByteArray[0], Length(ByteArray));
// Check to see if the BinData contains the expected bytes.
Memo1.Lines.Add(CkBinData__getEncoded(bd,'hex'));
// Get the binary bytes from the BinData
DataPtr := CkBinData_GetData(bd);
DataLength := CkBinData_getNumBytes(bd);
// Check for valid result
if (DataPtr <> nil) and (DataLength > 0) then
begin
SetLength(ByteArray2, DataLength);
Move(DataPtr^, ByteArray2[0], DataLength);
end
else
ByteArray2 := nil;
// Check to see if we got the bytes.
CkBinData_Clear(bd);
CkBinData_AppendData(bd,@ByteArray2[0], Length(ByteArray2));
Memo1.Lines.Add(CkBinData__getEncoded(bd,'hex'));
// ---------------------------------------------------------------
// Instead of getting the full contents of the BinData, just get a portion.
// Get the 3-byte chunk starting at index 2.
// The result should be 2,3,4
DataPtr := CkBinData_GetDataChunk(bd,2,3);
SetLength(ByteArray3, 3);
Move(DataPtr^, ByteArray3[0], 3);
// Check to see if we got the bytes.
CkBinData_Clear(bd);
CkBinData_AppendData(bd,@ByteArray3[0], Length(ByteArray3));
Memo1.Lines.Add(CkBinData__getEncoded(bd,'hex'));
// Dispose of the Chilkat BinData object.
CkBinData_Dispose(bd);
end;