The byte order refers to the order in which multibyte data is stored in the computer's memory or when it is transmitted over the network. The following are the main 2 kinds of common:
- Small-endian (Little-endian): Low-bit bytes are emitted at the lower address end of the memory that is the starting address of the value, and high-order bytes are emitted at the upper address of the memory. The most in line with the human mind of the byte sequence, address low storage value of low, address high storage value high. This sequence because from the person's first impression low value, should be placed in the memory address is small, that is, the memory address is low, conversely, the high value should be placed in the memory address large place, that is, memory address high. On the 80x86 platform, the system stores the low-level of the multibyte in the variable start address, using the small-end method.
- Big-endian (Big-endian): High byte emissions at the low address end of memory that is the starting address of the value, low bytes emitted at the high address of the memory. The most intuitive byte order, address low storage value of high, address high storage value low. This sequence does not need to consider the corresponding relationship, just to the memory address from left to right according to the order from low to high to write the value in the order of the usual high to low, the comparison between the two, a byte of a byte of the fill in.
The common one is the network order, that is, the network byte order, it is the TCP/IP specified a good data representation format, it is independent of the specific CPU type, operating system and so on, so as to ensure that the data in the transmission between different hosts can be interpreted correctly, network byte order using the big endian ordering method.
In the C language, in order to convert, the socket library provides conversion functions, including the following four:
- Htons: Converting the unsigned short type from the host sequence to the network sequence
- HTONL: Converting unsigned long from host to network order
- Ntohs: Converting the unsigned short type from the network sequence to the host order
- Ntohl: Converting unsigned long from network order to host sequence
In systems using little endian, these functions convert the byte order, and in systems using the big endian type, these functions are defined as empty macros without any conversion. For example, for integer 0x12345678, on Windows systems (small-endian storage), use the C and Python languages to see their different representations. Its bit [0-31] is represented by: the small End sequence (Little-endian) and the big-endian (Big-endian) are represented by: their network byte-order is the same as that of the endian. (1) C language Program:
1#include <iostream>2#include <WinSock.h>3 #pragmaComment (lib, "Ws2_32")4 using namespacestd;5 6 voidMain ()7 {8 intA =0x12345678;9cout<<"A ="<<showbase<Endl;Tencout<<"Little Endian: ["; One for(intI=0; i<4; i++) A { -cout<<int(*((Char*) &a+i) <<" "; - } thecout<<"]"<<Endl; - - intb =htonl (a); -cout<<"Big Endian: ["; + for(intI=0; i<4; i++) - { +cout<<int(*((Char*) &b+i) <<" "; A } atcout<<"]"<<Endl; - - GetChar (); -}
The result of this operation is:
In the debug state, you can see the address of the variable A and variable B in the Memory window, as well as a clear view of the different storage conditions of the two byte sequences. In this example, the addresses of the variables A and B are 0x00b3fd3c and 0X00B3FD24, respectively:
(2) Python language program:
1 #-*-coding:utf-8-*-#2 3 #-------------------------------------------------------------------------------4 #name:littleandbigendiantest5 #Description:6 #Author:administrator7 #DATE:2018/7/88 #-------------------------------------------------------------------------------9 Ten ImportSYS One fromstructImport* A fromcTYPESImport* - ImportNumPy as NP - theA = 0x12345678 - Print('A:'+Hex (a)) - forName, FMTinchZip (['Littleendian:','Bigendian:','Network:'], [Struct ('<1i'), Struct ('>1i'), Struct ('!1i')]): -Buffer =Create_string_buffer (fmt.size) + fmt.pack_into (buffer, 0, a) -data = Np.frombuffer (buffer, dtype=np.uint8) + Print(name + str (MAP (LambdaX:hex (x), data)))
The result of this operation is:
The result is consistent with the C language version, and also verifies that the network byte order is identical to the big-endian result.
BYTE-Order Understanding----C and Python languages