Reprinted from: http://www.xuanyusong.com/archives/1919
The client interacts with the server, using JSON and XML to feel that the amount of data is too large to affect efficiency. Using binary can save a lot of space.
The core class used when writing binary data is BinaryWriter, binary is binary meaning, visible operation binary write is used BinaryWriter. The commonly used data type allocates a fixed number of bytes, assuming that BinaryWriter writes a short then takes up 2 bytes, writes an int that takes up 4 bytes, and if it is an array, the array type byte length is multiplied by the array length.
byte: One byte (8 bits)
Short : Two bytes (16 bits)
int: Four bytes (32 bits) (one word length)
Long: Eight bytes (64 bits)
float: Four bytes (32 bits)
Double: eight bytes (64 bits)
and then talking about string, it's not a standard data type, it's an object, and its byte length is mutable. Also , BinaryWriter writes the length of the string as a byte, and then stores the length of the string in bytes. In parsing the string, you need to parse the string length and then convert the byte array to string by taking a byte array of the corresponding length after the length is obtained. When we write, we use BinaryWriter to read the BinaryReader.
C # and Unity memory optimization