First, it is clear that the Members in the struct are arranged from the low address to the high address, which is similar to the array, that is, the members in front of the struct occupy the low memory address, the member behind the struct occupies a high memory address.
Union int_char {
Int;
Char B [4];
} Ton;
Ton. A = 0x02ff03ff;
Printf ("% d/N", Ton. B [0], Ton. B [1], Ton. B [2], Ton. B [3]);
Output:
-1 3-1 2
B [1] Stores 0x03, B [3] Stores 0x02, and for the integer 0x02ff03ff, its high byte 0x02 is placed in B [3], B [3] occupies the high address of the memory, that is, the high address of the high byte in the memory, so it is in the small byte order. My machine is also x86, which is consistent with the machine's byte order.
Let's look at the IP address representation method:
Struct in_addr {
Union {
Struct {uchar s_b1, s_b2, s_b3, s_b4;} s_un_ B;
Struct {ushort s_w1, s_w2;} s_un_w;
Ulong s_addr;
} S_un;
If s_un.s_addr = 2171906240; // (0x8174a8c0)
The corresponding IP address is 192.168.116.129, and the output format is: printf ("% d. % d. % d. % d \ n ", s_un.s_un_ B .s_b1, s_un.s_un_ B .s_b2, s_un.s_un_ B .s_b3, s_un.s_un_ B .s_b4); the output is correct.
It is also the low address (s_b1) of the memory corresponding to the low byte (such as 0xco ).
This is also observed on my machine, which is consistent with the byte order of X86. Generally, the network programming transmission order is the network byte order, that is, the large-end byte order. Therefore, the machine with a small byte sequence should be converted accordingly.
A) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.
B) Big-Endian refers to the low address of the memory where the high byte is discharged, and the low byte is discharged to the high address of the memory.
C) network byte sequence: Network Transmission generally adopts the large-End sequence, also known as the network byte sequence or network sequence. In the IP protocol, the large client order is defined as the network byte order.
Http://zh.wikipedia.org/zh-cn/%E5%AD%97%E8%8A%82%E5%BA%8F