First, why does the size end mode appear? Different CPU sizes are inconsistent with the size-end mode.
The X86 is a small-end mode. and Keil C51 is the big-endian mode. Very much arm. DSP is a small-end mode.
Some arm processors can also be hardware to choose between big-endian or small-end mode.
Two, the difference between the size and end of the problem is what? How to solve? Assume that there is a data network transmission. Assume that the size-end pattern is inconsistent, assuming no conversion. Must result in no data, no errors. solution: Unify the byte sequence transmitted on the network in the same mode (as everyone knows), so that when sending and receiving data, is based on whether the corresponding mode of the host is consistent with the corresponding pattern of the network bytes, to infer whether the conversion is required.
This way, the network transmission does not fail, even if different devices use different modes.
third, how to infer the CPU size end mode? There are several ways to list three kinds. (1), the use of joint//Return to True, is the small endbool Littleendian (){Union {int i;char c;}udata;udata.i = 1;return (UDATA.C = = 1);}
(2), through the pointer inferencebool Littleendian (){int d = 0x12345678;char* p = * (char*) &d;return (*p = = 0x78);}
(3), in the Linux environment, through the htonl and other functions directly inferred. #include <arpa/inet.h>bool Littleendian (){return (1!=HTONL (1));}
Iv. network byte order
Because different systems will have different patterns, in order to unify. Specifies the use of big-endian mode in network transmissions. This is the network byte order. Now look at the effect of these four functions
uint32_t htonl (uint32_t hostlong); //32-bit host byte-order conversion to network byte order uint16_t htons (uint16_t hostshort); //16-bit host byte-order conversion to network byte order uint32_t Ntohl (uint32_t netlong); //32 bit of network byte order translates to host byte order uint16_t Ntohs (uint16_t netshort); //16-bit network byte-order conversion to host byte-order
Take htonl and Ntohl to analyze, htonl function of the internal implementation principle is this, first infer what the host is the mode of storage, assuming that the big-endian mode, and the network byte-order consistent, direct return to the parameters can be, assuming that the small-end mode. The conversion of the shape to the big-endian mode is stored in a temporary parameter, and then the temporary parameters are returned, and the Ntohl function is the same as the implementation of the principle is the same process, but to pay attention to its parameters, the parameter is the network byte order, is the big-endian mode of storage, regardless of the process you pass into the actual participation is So when you infer that the host is in big-endian mode. is returned directly, since the function defaults to the fact that the form is a network byte order, which is seen as the big-endian mode. Assuming the inference host is a small-ended mode, the actual participation is converted, the process of conversion is not complicated, that is, in reverse order to store each byte of data, so the result is converted.
Here, you can see a pattern. is to assume that the host and network byte order is inconsistent (that is, the small-end mode), the return value of these four functions and the value of the passed in the byte ordering is definitely reverse, so the return value is definitely not equal to the actual value. For example, the result of htonl (1) is certainly not 1, and assuming that the host and network byte-order (that is, the big-endian mode), then the four functions do not do the conversion operation, but directly return the actual value, so that their return is certainly the same as the actual value, that is, htonl (1) The result is 1.
In this way, we get a very simple way to infer what the system is. is to use these four functions directly to infer. Such as
if (1 ! = htonl (1)) {// Small-end mode, which should handle the else {//big- endian mode, to handle }
or directly with an inferred if (1! = htonl (1)). Only if the host byte order is inconsistent with the network byte order, those function conversions are called, otherwise, no treatment is required, which can reduce the additional function calls.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Size in terminal mode and network byte order