Click or drag to resize

BinaryTextEncodingLengthEncoded Method

Returns an encoder that will encode and decode length encoded string.

Namespace: Demo3D.IO
Assembly: Demo3D.IO (in Demo3D.IO.dll) Version: 19.01.00
Syntax
C#
public static BinaryTextEncoding LengthEncoded(
	int headerLength,
	bool littleEndian,
	Encoding characterEncoding
)

Parameters

headerLength  Int32
The number of bytes that describe the length of the string that follows in the data buffer.
littleEndian  Boolean
The format of the bytes that describe the length of the string.
characterEncoding  Encoding
The character encoding.

Return Value

BinaryTextEncoding
An instance of BinaryTextEncoding that will encode and decode strings in this format.
Example

The following example shows simple examples for reading and writing strings.

C#
public void LengthEncodedStrings(IDataReader receivedPacket, IDataWriter packetToSend) {
    // Length encoded strings are prefixed with a binary number that states exactly how many
    // bytes belong to the string.  Most commonly, the number is itself 2-bytes (a 16-bit number),
    // and that number is itself encoded using the Big Endian format.
    var str = receivedPacket.ReadString(BinaryTextEncoding.LengthEncodedASCII2BE);

    // This example uses a 4-byte (32-bits) Little Endian number to describe the number of bytes
    // in the string.  The string is then interpretted as Unicode.
    str = receivedPacket.ReadString(BinaryTextEncoding.LengthEncoded(4, true, System.Text.Encoding.Unicode));

    // Writes the string "hello" into a packet ready to be sent to the peer.  The string is prepended with
    // two bytes 0x00, 0x05 (the length of the string).
    packetToSend.WriteString("hello", BinaryTextEncoding.LengthEncodedASCII2BE);
}
See Also