The recent use of C # for network development requires processing of ISO8583 messages, since some of these fields are numeric, thus involving byte-order conversions when transmitting.
Byte order refers to the order in memory of data that takes up more than one byte of memory, usually in two byte order, depending on where they are located we are called the host node Order and the network byte order respectively.
Usually we think the network byte order is the standard order, when the packet is marshaled, the host byte order is converted to the network byte order, when the packet is split, the network byte order is converted to the host byte order.
Originally thought also to write their own functions, in fact, the network library has been provided.
Host to Network: Short/int/long Ipaddress.hosttonetworkorder (Short/int/long)
Network to host: Short/int/long Ipaddress.networktohostorder (Short/int/long)
Host byte order refers to low-byte data stored at low memory addresses, high-byte data stored at high memory addresses, such as:
int x=1; At this point x is the host byte order: [1][0][0][0] low to high
int y=65536//At this time y is the host byte order: [0][0][1][0] low to high
We convert x and y through the host-to-network byte-order conversion function to get their corresponding network byte-order values,
The network section order is that high-byte data is stored at low addresses, and low-byte data is stored at high addresses, such as:
int M=ipaddress.hosttonetworkorder (x);
At this point m is host byte order: [0][0][0][1] High to low
int N=ipaddress.hosttonetworkorder (y);
At this point n is the host byte order: [0][1][0][0] High to low
After the conversion, we can go through
Byte[]btvalue=bitconverter.getbytes (m);
Get a byte array of length 4, and then set the array to the corresponding location of the message to send out.
Similarly, after receiving the message, the message can be split by domain, get btvalue, use
int M=bitconverter.toint32 (btvalue,0);//Start conversion from No. 0 bit of Btvalue
The value of the domain is not available at this time, and it should be converted with a network-to-host byte-order conversion function:
int X=ipaddress.networktohostorder (m);
The resulting x is the actual value in the message.
PS: Network byte order and host byte order
Different CPUs have different byte-order types these byte-order refers to the order in which integers are stored in memory this is called the host order.
Two of the most common
1. Little endian: Storing low-order bytes at the start address
2. Big Endian: Stores high-order bytes at the start address
LE Little-endian
The byte order that best fits the human mind
Low store value of address low
High-level storage value of address high
How to say is the most consistent byte-order of human thinking, because from the first impression of people
Low value, should be placed in the memory address is small, that is, memory address low
Conversely, the high value should be placed in the memory address is large, that is, memory address high
Be Big-endian
The most intuitive byte-order
High position of address low store value
Address High store value low
Why speak intuitively, don't consider correspondence
Just write the memory address from left to right in order from low to high
Write the values in the usual high-to-low order
In contrast, a byte-by-byte padding
Example: How to store a double-character 0x01020304 (DWORD) in memory
Memory address
4000 4001 4002 4003
LE 04 03 02 01
be 01 02 03 04
Example: If we write 0X1234ABCD to memory starting with 0x0000, the result is
Big-endian Little-endian
0x0000 0x12 0xCD
0x0001 0x23 0xAB
0x0002 0xAB 0x34
0x0003 0xCD 0x12
The x86 series CPUs are Little-endian byte-order.
Network byte order is a well-defined data representation format in TCP/IP, which is independent of the specific CPU type, operating system and so on, so that the data can be interpreted correctly when transferring between different hosts. The network byte order takes the big endian sort method.
In order to convert the BSD socket provides a conversion function with 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 unsigned short type from network order to host order
Ntohl converting unsigned long from network order to host sequence
In systems that use little endian, these functions convert the byte sequence
These functions are defined as empty macros in systems that use the big endian type.
Also in the network program development or cross-platform development should pay attention to ensure that only one byte sequence or the interpretation of the two sides is not the same will produce a bug.
Note:
1, network and host byte conversion function: Htons ntohs htonl Ntohl (S is short L is long H is host n is network)
2, different CPUs running different operating systems, byte order is also different, see the table below.
Processor OS byte sequencing
Alpha all Little endian
Hp-pa NT Little Endian
Hp-pa UNIX Big Endian
Intelx86 all Little Endian <-----x86 system is a small-endian byte-order system
motorola680x () all Big endian
MIPS NT Little Endian
MIPS UNIX Big Endian
PowerPC NT Little Endian
PowerPC non-NT Big endian <-----PPC system is endian system
rs/6000 UNIX Big Endian
SPARC UNIX Big Endian
IXP1200 Arm Core All Little endian
Conversion of C # host and network byte order about network byte order and host byte order