BinaryTextEncodingLengthEncoded Method |
Namespace: Demo3D.IO
public static BinaryTextEncoding LengthEncoded( int headerLength, bool littleEndian, Encoding characterEncoding )
The following example shows simple examples for reading and writing strings.
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); }