Conversion of host and network word sequence
Recently, use C # to develop network development, you need to process ISO8583 packets, because some of which are numerical, so that the word sequencing is involved in the transmission. The byte order is the order in which data is more than one byte type is stored in memory, usually there are two byte sequences, according to their location, we are called host sequence and network word sequence, respectively.
Usually we believe that the network word sequence is a standard order, and the host word sequencing is converted to a network word sequence, and the network word sequencing is converted to the host word sequence. I thought I had to write a function yourself. In fact, the network library has been provided.
Host to the network: Short / int / long ipaddress.hosttonetworkOrder (Short / INT / Long)
Network to the host: short / int / long ipaddress.networktohostorder (short / int / long): SHORT / INTWORKTOHOSTORDER (Short / INT / Long)
Host word sequence Individual Low byte data is stored at the memory low address, high byte data is stored at the memory high address, such as:
INT x = 1; // At this time, X is the host word sequence: [1] [0] [0] [0] low to high
INT Y = 65536 // This time y is the host word sequence: [0] [0] [1] [0] low to high
We convert X and Y through the transition function of the host to the network word sequence to get the network byte sequence value of their corresponding network, and the network sequence is high byte data stored at the low address, the low-byte data is stored in the high address. Position, such as:
INT M = ipaddress.hosttonetworkOrder (x);
/ / At this time, M is the host word sequence: [0] [0] [0] [1] high to low
INT n = ipaddress.hosttonetworkOrder (Y);
// This time n is the host word sequence: [0] [1] [0] [0] high to low
After the conversion, we can pass
Byte [] btvalue = bitconverter.getbytes (m);
Get a BYTE array of length 4, and then set this array to the corresponding position of the message to send it.
Similarly, after receiving the message, you can split the message according to the domain, get Btvalue, use
INT m = bitconverter.Toint32 (btvalue, 0); // Spend from the 0th bit of Btvalue
Get the value of this domain, it is not possible to use it directly, and the network to the host word sequence should be converted:
INT x = ipaddress.networktohostorder (m);
The X obtained at this time is the actual value in the packet.
The first time I use C # to do projects, and I have issued the text in the first time. I am afraid to enter the public, I urge to enlighten me.