ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Delphi Binary Serialization for Data Transfer

Delphi / Object Pascal

Goal -- WPM

Ready
Exercise Algorithm Area
1type
2TMyData = record
3ID: Integer;
4Name: string;
5IsEnabled: Boolean;
6end;
7
8procedure WriteInteger(AStream: TStream; AValue: Integer);
9begin
10AStream.WriteBuffer(AValue, SizeOf(AValue));
11end;
12
13procedure WriteBoolean(AStream: TStream; AValue: Boolean);
14var
15B: Byte;
16begin
17if AValue then B := 1 else B := 0;
18AStream.WriteBuffer(B, SizeOf(B));
19end;
20
21procedure WriteString(AStream: TStream; AValue: string);
22var
23Len: Integer;
24Bytes: TBytes;
25begin
26Len := Length(AValue);
27WriteInteger(AStream, Len); // Write string length first
28if Len > 0 then
29begin
30Bytes := TEncoding.UTF8.GetBytes(AValue);
31AStream.WriteBuffer(Bytes[0], Length(Bytes));
32end;
33end;
34
35procedure SerializeMyData(AStream: TStream; const AData: TMyData);
36begin
37WriteInteger(AStream, AData.ID);
38WriteString(AStream, AData.Name);
39WriteBoolean(AStream, AData.IsEnabled);
40end;
41
42// Example usage:
43// var
44// MemStream: TMemoryStream;
45// MyRecord: TMyData;
46// begin
47// MemStream := TMemoryStream.Create;
48// try
49// MyRecord.ID := 123;
50// MyRecord.Name := 'Example String';
51// MyRecord.IsEnabled := True;
52// SerializeMyData(MemStream, MyRecord);
53// // MemStream now contains the binary representation
54// finally
55// MemStream.Free;
56// end;
57// end;
Algorithm description viewbox

Delphi Binary Serialization for Data Transfer

Algorithm description:

This Delphi code provides functions to serialize a simple record (`TMyData`) into a binary format, which can then be written to a `TStream` (like `TMemoryStream` or `TFileStream`). It defines helper procedures to write primitive types such as integers, booleans, and strings. For strings, it first writes the length, followed by the UTF-8 encoded byte representation of the string. This binary serialization is useful for saving application state, transferring data between processes, or sending data over a network.

Algorithm explanation:

The `SerializeMyData` procedure serializes a `TMyData` record. It calls helper procedures `WriteInteger`, `WriteBoolean`, and `WriteString` in a specific order to ensure consistent data representation. `WriteInteger` and `WriteBoolean` directly write the memory representation of the types to the stream. `WriteString` is more complex: it first writes the length of the string as an integer, then converts the string to a UTF-8 byte array and writes those bytes. This length prefix is crucial for deserialization, allowing the reader to know how many bytes to consume for the string. The order of serialization must be strictly maintained during deserialization. The time complexity is O(L) where L is the length of the string, as string encoding and writing dominate. Space complexity is O(L) for the temporary byte array used for string encoding. This approach is endianness-agnostic if `WriteBuffer` handles it correctly or if explicit byte swapping is implemented for cross-platform compatibility.

Pseudocode:

TYPE TMyData = RECORD
  ID: Integer
  Name: String
  IsEnabled: Boolean
END RECORD

PROCEDURE WriteInteger(Stream, Value):
  Write Value (as bytes) to Stream
END PROCEDURE

PROCEDURE WriteBoolean(Stream, Value):
  Convert Value to 0 or 1 byte
  Write byte to Stream
END PROCEDURE

PROCEDURE WriteString(Stream, Value):
  Len = Length(Value)
  Call WriteInteger(Stream, Len)
  IF Len > 0 THEN
    Convert Value to UTF-8 bytes
    Write UTF-8 bytes to Stream
  END IF
END PROCEDURE

PROCEDURE SerializeMyData(Stream, Data):
  Call WriteInteger(Stream, Data.ID)
  Call WriteString(Stream, Data.Name)
  Call WriteBoolean(Stream, Data.IsEnabled)
END PROCEDURE