I know the size of the end, but I have not sorted it out. I only know that the network needs big-Endian, that is, the big end mode.
In various computer architectures, the storage mechanisms for bytes and words are different, which leads to a very important issue in the computer communication field, that is, the order in which information units (bit, byte, word, double-word, etc.) of communication between the communication parties should be transmitted. If no agreement is reached, the communication fails due to incorrect encoding/decoding. At present, there are two types of byte storage mechanisms commonly used in computer systems: Big-Endian and little-Endian.
Taking unsigned int value = 0x12345678 as an example, we can use unsigned char Buf [4] to show the storage conditions of the two types of bytes respectively:
Big-Endian: high storage for low addresses, for example:
Stack bottom (high address)
---------------
Buf [3] (0x78) -- low
Buf [2] (0x56)
Buf [1] (0x34)
Buf [0] (0x12) -- high
---------------
Stack top (low address)
Little-Endian: Low-address storage, such:
Stack bottom (high address)
---------------
Buf [3] (0x12) -- high
Buf [2] (0x34)
Buf [1] (0x56)
Buf [0] (0x78) -- low
--------------
Stack top (low address)
Big-Endian advantages: You can always determine whether the number is positive or negative by first extracting the high byte. You don't have to know how long the value is, or you don't have to take some bytes to check whether the value contains a symbol. These values are stored in the order they are printed, so the function from binary to decimal is particularly effective. Therefore, different access methods are designed for machines with different requirements.
Little-Endian advantages: extract one, two, four or more bytes of data assembly instructions in the same way as all other formats: first, extract the byte of the second digit in the place where the offset address is 0. Because the address offset and number of cell lines are one-to-one, the mathematical functions with multiple precision are relatively easy to write.
How do I check whether the processor is big-Endian or little-Endian?
Because the Union storage sequence is that all members are stored from a low address, this feature allows you to easily read and write the memory in little-Endian or big-Endian mode. For example:
#include <stdio.h>int checkCPUendian(){ union { unsigned int a; unsigned char b; }c; c.a = 1; return (c.b == 1); } /*return 1 : little-endian, return 0:big-endian*/int main(){ checkCPUendian() ? printf("Little-endian\n"):printf("Big-endian\n"); return 0;}
Or the most direct:
# Include <stdio. h> int main () {short int X; char x0, x1; X = 0x1122; X0 = (char *) & X) [0]; // low address unit X1 = (char *) & X) [1]; // high address unit if (X0 = 0x11) printf ("big-Endian \ n"); else printf ("Little-Endian \ n"); Return 0 ;}