network byte order

1. Concept:

  Big-endian mode (Big-endian) : It means that the high byte of the data is stored in the low address of the memory, and the low byte of the data is stored in the high address of the memory. This storage mode is a bit similar to treating the data as String sequence processing: the address increases from small to large, and the data is placed from high to low; this is consistent with our reading habits.

  Little-endian mode (Little-endian) : means that the high byte of the data is stored in the high address of the memory, and the low byte of the data is stored in the low address of the memory. Effectively combined, the high address part has a high weight, and the low address part has a low weight.

  In little-endian byte order and big-endian byte order, the difference is whether the address is stored in the low-order or high-order. The low and high bits in the byte order are only for the combined memory space greater than 1 byte (that is, the size of the value is composed of multiple memory units, such as int is 4 bytes (32-bit or 64-bit), Then the value of type int is obtained by combining 4 memory units). The unit of memory is bytes. For characters, char is 1 byte, which is not affected by the host byte order and network byte order. There is only one unit in memory, and there is no distinction between before and after. However, when combining memory space, because there are multiple memory units, there are front and rear points, and the difference between little endian and big endian byte order is how to combine the front and rear memory units. In little-endian byte order, the low-order bits (ie, low addresses) are stored in the first memory unit, and the high-order bits are stored in the next memory units in turn. In big-endian byte order, the high order (ie low address) is stored in the first memory unit, and the low order is stored in the next memory units in turn.

Example:

  The integer value is 10000, and the corresponding hexadecimal representation is 0x2710. According to the byte, it can be divided into 0x27 (high order) and 0x10 (low order)
little-endian byte order: the first memory unit is the low address, and the low order is stored: 0x10, the next The memory unit is the high address, storing the high bit: 0x27 ---> 0x1027
big endian byte order: the first memory unit is the low address, storing the high bit: 0x27, the next memory unit is the low address, storing the low bit: 0x10 - --> 0x2710 The
  array is allocated memory continuously, the storage space of each array unit is determined by the array type, but the sequence of the array units is fixed. Then after the network transmission, the order of the elements of the array will not change. As for the value of the array element, if the array type occupies more than 1 byte, it will change. The string is a character array, and the character occupies 1 byte, so the final string value will not be affected if the receiving end does not perform the endian conversion.
  During network transmission, it is necessary to transmit in big-endian byte order. In fact, it is to avoid the inconsistency of the local byte order on both sides of the machine that sends the data and the machine that receives the data, resulting in inconsistent data received. If the byte order and the network byte order are the same, then the endian conversion is not required. Otherwise, the endian conversion is performed.

 

2. Conversion

2.1. Small head to big head: System.Net.IPAddress.HostToNetworkOrder(value)//NET is a small head structure, and network bytes are a large head structure, which needs to be agreed between the client and the server

2.2, turn the big head to the small head:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324445140&siteId=291194637