Linux Network Programming: in bytes (Large End, Small End, network, host), linux Large End
Byte order: the order in which data is stored in the memory. It can also be called the end mode.
Definition of big-end mode and small-end Mode
1)Little-EndianIt means that the low byte is discharged at the low address end of the memory, and the high byte is discharged at the high address end of the memory.
2)Big-EndianThat is, high bytes are discharged at the low address end of the memory, and low bytes are discharged at the high address end of the memory.
3)Network byte order: TCP/IP Protocols define byte orderBig-EndianTherefore, the byte order used in TCP/IP is usually called the network byte order.
What is a high byte or a low byte?
Generally, a 16-bit (dual-byte) data, such as FF1A (hexadecimal), is represented by a four-bit binary system in hexadecimal format)
So the high byte is FF, and the low byte is 1A.
For 32-bit data, such as 3f681_ B
The value of a high-level character (not a byte) is 3F68.
The low position is limit B.
Low on the right and high on the left
Include <stdio. h> # include <arpa/inet. h> int main (int argc, char * argv []) {unsigned int x = 0x12345678; unsigned char * p = (unsigned char *) & x; // small-end bytecode printf ("% p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n ", p + 0, * (p + 0), p + 1, * (p + 1), p + 2, * (p + 2), p + 3, * (p + 3); printf ("--------------------- \ n"); // h: Host byte order n: Network byte order l: long unsigned int y = htonl (x); p = (unsigned char *) & y; printf ("% p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n ", p + 0, * (p + 0), p + 1, * (p + 1), p + 2, * (p + 2), p + 3, * (p + 3); return 0 ;}
Output result:
0x7fff3bb55708-> 78
0x7fff3bb55709-> 56
0x7fff3bb5570a-> 34
0x7fff3bb5570b-> 12
---------------------
0x7fff3bb5570c-> 12
0x7fff3bb5570d-> 34
0x7fff3bb5570e-> 56
0x7fff3bb5570f-> 78
Result 1:
0x7fff3bb55708-> 78
0x7fff3bb55709-> 56
0x7fff3bb5570a-> 34
0x7fff3bb5570b-> 12
The address is increased from 08 ---> 0b. The low address of 08 is 78, and the number of 78 is low. In this case, the low address is put in the low byte, and the high address is put in the high byte, so it is called: Small-end byte order
Result 2:
0x7fff3bb5570c-> 12
0x7fff3bb5570d-> 34
0x7fff3bb5570e-> 56
0x7fff3bb5570f-> 78
Converts the host's byte order to the network's byte order. In this case, the low address places the high byte, and the high address places the low byte order.
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------
APIS related to byte sequence conversion:
Htonl: the host's byte order is converted to the network's byte order.
Inet_addr: function prototype: in_addr_t inet_addr (const char * cp)
Purpose: Convert the IP address format to network byte
unsigned int addr = inet_addr( "192.168.1.100" );printf( "%u\n", addr );printf( "0x%0x\n", addr );
Result:
1677830336
0x6401a8c0
The two numbers are the same.
Ntohl: Network byte order to host byte order
unsigned int haddr = ntohl( addr );printf( "%u\n", haddr );printf( "0x%0x\n", haddr );
Inet_ntoa: Network byte order, to IP address format
struct in_addr ipaddr;ipaddr.s_addr = addr;printf( "%s\n", inet_ntoa( ipaddr ) );