Byte and network byte

Source: Internet
Author: User
Tags htons
1 byte orderBecause different computer systems store data in different byte order and a 4-byte 32-bit integer, the memory storage method is different. the byte order is divided into the Little Endian and the Big Endian. Most Intel processors use the Little Endian, and most Motorola processors use the Big Endian; the small tail is the low byte emission at the low end of the memory, and the high byte emission at the high end of the memory. For example, a 4-byte integer with a value of 0x1234567 corresponds to a high/low byte:
01 23 45 67
Byte3 Byte2 Byte1 Byte0
High byte -- à --------- à -------------- à low byte
It will be discharged in the memory in the following order:
Memory Address No. Byte address in memory Hexadecimal value
0x03 Byte3 01
0x02 Byte2 23
0x01 Byte1 45
0x00 Byte0 67
The big tail is the low-end memory for high bytes and the high-end memory for low bytes. For example, a 4-byte integer with a value of 0x1234567 corresponds to a high/low byte:
01 23 45 67
Byte3 Byte2 Byte1 Byte0
High byte -- à --------- à -------------- à low byte
It will be discharged in the memory in the following order:
Memory Address No. Byte address in memory Hexadecimal value
0x03 Byte0 67
0x02 Byte1 45
0x01 Byte2 23
0x00 Byte3 01

 

2 network byte orderEach layer of TCP/IP protocol defines the byte order as the end. Therefore, the byte order used in TCP/IP protocol is generally called the network byte order. 3 storage of strings in memory (intel Series) The string is the opposite of an integer. The index of an string is stored in the memory from low to high. char s [4] = "abc ";
A B C /0
S [0] S [1] S [2] S [3]
 
It will be discharged in the memory in the following order:
Memory Address No. Hexadecimal value Position of pointer P
0xbffeadf7 /0 P + 3
0xbffeadf6 C P + 2
0xbffeadf5 B P + 1
0xbffeadf4 A P
Int main (void) {char s [4] = "abc"; char * p = s; printf ("% 02x, % 02x, % 02x, % 02x, % 02x/n ", & s [0], & s [1], & s [2], & s [3]); printf ("% 02x, % 02x, % 02x, % 02x/n ", p, p + 1, p + 2, p + 3); printf (" % c, % c/n ", s [0], s [1], s [2], s [3]); return 0;} output result: [netcool @ HFINMSP2 demo] $. /demo001bffeadf4, bffeadf5, bffeadf6, bffeadf7bffeadf4, bffeadf5, bffeadf6, bffeadf7a, B, c, 4. Storage of integer arrays in memory (intel Series)The same as the string, but the storage of each integer in the array is in the byte order;   5. Handling Methods in linuxThe Network byte sequence is a standard byte sequence. if the system does not provide the relevant conversion functions, we can use the following four macros to implement mutual conversion between the local byte sequence and the network byte sequence: htons (): converts a 16-bit unsigned integer from the local byte order to the network byte order htonl (): converting a 32-bit unsigned integer from a local byte to a network byte ntohs (): converting a 16-bit unsigned integer from a network byte to a local byte ntohl (): converting 32-bit unsigned integers from network byte order to local byte order
Note: 1. network and host byte Conversion Function: htons ntohs htonl ntohl (s means that short l is long h is host n is network) 2. different operating systems run on different CPUs in different bytes. See the following table. Processor operating system byte sorting Alpha all Little endianHP-pa nt Little endianHP-pa unix Big endianIntelx86 all Little endian <----- x86 systems are small-end byte ordering systems except la680x () all Big endianMIPS NT Little endianMIPS UNIX Big endianPowerPC NT Little endianPowerPC non-NT Big endian <----- PPC system is a large-end bytecode system RS/6000 UNIX Big endianiscsi UNIX Big endianIXP1200 ARM core all Little endian

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.