Since converting between untyped and typed binary data is important for streams, TQTXStream inherits directly from TDataTypeConverter. This means you do not need to create a separate instance of the converter if there is a stream involved. Your stream object is the converter, and all its powerful methods are available directly on the stream instance you are already working with.

This inheritance abstracts away the need for manual, error-prone bitwise operations (shifting and masking) to pack and unpack data.


Practical Example: Using the Stream as a Converter


uses

 qtx.sysutils, qtx.classes;


procedure SaveData(Stream: TQTXStream);

var

 PlayerScore: int32;

 PlayerName: string;

 NameBytes: TUInt8Array;

begin

 PlayerScore := 1500;

 PlayerName  := 'Nadia';


 // No separate converter instance is needed!

 // TQTXStream IS a TDataTypeConverter.


 // Convert data from Pascal types to bytes using the stream itself

 NameBytes := Stream.StringToBytes(PlayerName);


 // Write the raw bytes to the stream

 // (Note: We must also save the length of the string)

 Stream.Write( Stream.Int32ToBytes(NameBytes.Length) );

 Stream.Write(NameBytes);

 Stream.Write( Stream.Int32ToBytes(PlayerScore) );

end;