Mohamad
Mohamad

Reputation: 1117

Using TWriter.WriteString two extra bytes are saved

In Delphi 10.4.2, when I use the TWriter.WriteString two extra bytes are saved :

var
  FileStream: TFileStream;
  Writer: TWriter;
begin
    FileStream := TFileStream.Create('stream.txt', fmCreate or fmOpenWrite or fmShareDenyNone);
    Writer := TWriter.Create(FileStream, $FF);
    try
      Writer.WriteString('2');
    finally
      Writer.Free;
      FileStream.free;
    end
end;

enter image description here

What are these two bytes? How can I ignore them?

Upvotes: 1

Views: 293

Answers (1)

fpiette
fpiette

Reputation: 12292

This is by design of TWriter.WriteString. Probably your use case is not the correct one.

The first byte ($06) is the value type (TValueType.vaString for your code). The second byte is the length of the string (1 byte for you).

You can find all that information in the source code provided by Embarcadero in file System.Classes.pas.

You cannot ignore them. Maybe you can use TStream.Write to write your string without extra payload?

Upvotes: 5

Related Questions