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: 15.0.2.11458
Syntax
C#
public static BinaryTextEncoding LengthEncoded(
	int headerLength,
	bool littleEndian,
	Encoding characterEncoding
)

Parameters

headerLength
Type: SystemInt32
The number of bytes that describe the length of the string that follows in the data buffer.
littleEndian
Type: SystemBoolean
The format of the bytes that describe the length of the string.
characterEncoding
Type: System.TextEncoding
The character encoding.

Return Value

Type: BinaryTextEncoding
An instance of BinaryTextEncoding that will encode and decode strings in this format.
Examples

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