Click or drag to resize

BinaryTextEncodingLengthEncoded2ByteASCII Method

Returns an encoder that will encode and decode 2-byte length encoded ASCII string. The string is formatted in the data buffer with the first two bytes containing the length of the string that follows. The characters in the string are ASCII.

Namespace:  Demo3D.IO
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 11.0.0.7658
Syntax
C#
public static BinaryTextEncoding LengthEncoded2ByteASCII(
	bool littleEndian
)

Parameters

littleEndian
Type: SystemBoolean
The format of the bytes that describe the length of the string.

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.LengthEncodedASCII);

    // 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.LengthEncodedASCII);
}
See Also