BinaryTextEncodingFixedLength Method |
Namespace: Demo3D.IO
public static BinaryTextEncoding FixedLength( Encoding characterEncoding, ushort length = 0 )
The following example shows simple examples for reading and writing fixed/variable length strings.
// These first two examples show how to read/write variable-length strings to/from a packet. public void VariableLengthStrings(IDataReader receivedPacket, IDataWriter packetToSend) { // Reads all the bytes in 'receivedPacket' and converts it all into an ASCII string. var str = receivedPacket.ReadString(BinaryTextEncoding.FixedLengthASCII); // Writes the string "hello" into a packet ready to be sent to the peer. packetToSend.WriteString("hello", BinaryTextEncoding.FixedLengthASCII); } // Sometimes there's more data in a packet than just one string. In which case, it may be // necessary to specify exactly how long the string is that we're expecting to read. public void FixedLengthStrings(IDataReader receivedPacket, ushort lengthOfString) { // Reads 'lengthOfString' bytes of data from 'receivedPacket' and then interprets them as Unicode (UTF-16). var str = receivedPacket.ReadString(BinaryTextEncoding.FixedLength(System.Text.Encoding.Unicode, lengthOfString)); }