C # host/network byte sequence Conversion

Source: Internet
Author: User
Tags htons

Recently, I used C # for network development and needed to process iso8583 packets. Some of these fields are numeric, so the conversion of byte order is involved during transmission.

Byte order refers to the storage order of data that occupies more than one byte in the memory. there are usually two types of byte order, according to their location, we are called the host node order and the network byte order respectively.

We usually think that the network byte order is the standard order. When packets are sent, the host byte order is converted to the network byte order, and the network byte order is converted to the host byte order during packet splitting.

I thought I had to write the function myself. In fact, the network library already provides.

Host to network: Short/INT/long IPaddress. hosttonetworkorder (short/INT/Long)

Network to host: Short/INT/long IPaddress. networktohostorder (short/INT/Long)

The host's byte sequence means that the low-byte data is stored at the low-address in the memory, and the high-byte data is stored at the Internal High address, for example:

Int x = 1; // at this time, X is the host's byte order: [1] [0] [0] [0] low to high

Int y = 65536 // at this time, Y is the host's byte order: [0] [0] [1] [0] low to high

We use the host-to-network byte order conversion functions to convert X and Y to obtain their corresponding network byte values,

The Network collation stores high-byte data at the low address, and low-byte data is stored at the high address, for example:

Int M = IPaddress. hosttonetworkorder (X );

// At this time, M is the host's byte order: [0] [0] [0] [1] high to low

Int n = IPaddress. hosttonetworkorder (y );

// At this time, n is the host's byte order: [0] [1] [0] [0] high to low

After conversion, we can use

Byte [] btvalue = bitconverter. getbytes (m );

Get a byte array with a length of 4, and then set the array to the corresponding position of the message and send it out.

After receiving the packet, you can split the packet by domain to obtain the btvalue.

Int M = bitconverter. toint32 (btvalue, 0); // The conversion starts from the 0th bits of btvalue.

The value of this field cannot be directly used at this time. You should use the network-to-host byte Conversion Function to convert it:

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 sort of bytes. These sort of bytes refer to the order in which integers are stored in the memory. This is called the host order.
There are two most common
1. little endian: stores low-order bytes at the starting address
2. Big endian: stores High-Order bytes at the starting address.

Le little-Endian
The byte sequence that best fits people's thinking
Low-level address storage value
High address storage value
This is the byte sequence that best fits people's thinking, because it is from the perspective of human first.
If the low value is small, it should be placed where the memory address is small, that is, the low value of the memory address.
Otherwise, the high value should be placed in the place where the memory address is large, that is, the memory address is high.

Be big-Endian
The most intuitive byte order
High level of the low-level address storage value
The low storage value of the high address
Why is it intuitive? Do not consider mappings.
Write the memory address from left to right in ascending order.
Write the value in the order of high to low.
By contrast, one byte and one byte are filled in.

Example: Memory dual-word 0x01020304 (DWORD) Storage Method

Memory Address
4000 4001 4002 4003
Le 04 03 02 01
Be 01 02 03 04

Example: If we write 0x1234abcd to the memory starting with 0x0000, the result is
Big-Endian little-Endian
0x0000 0x12 0xcd
0x0001 0x23 0xab
0x0002 0xab 0x34
0x0003 0xcd 0x12
X86 series CPUs are in the byte order of little-Endian.

The Network byte sequence is a data representation format specified in TCP/IP. It has nothing to do with the specific CPU type and operating system, this ensures that data can be correctly interpreted during transmission between different hosts. The Network byte sequence adopts the big endian sorting method.

The following four conversion functions are provided for BSD socket conversion:
Htons converts the unsigned short type from host to Network
Htonl converts the unsigned long type from the host sequence to the network Sequence
Ntohs converts the unsigned short type from the network sequence to the host Sequence
Ntohl converts the unsigned long type from the network sequence to the host Sequence

In systems using little endian, these functions convert the byte order.
In systems using the big endian type, these functions are defined as empty macros.

During network program development or cross-platform development, you should also ensure that only one byte sequence is used. Otherwise, different interpretations of the two parties may cause bugs.

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 endian
HP-PA nt little endian
UNIX big endian HP-PA
Intelx86 all little endian <----- x86 systems are small-end bytecode Systems
Motorola680x () All big endian
MIPs nt little endian
MIPs UNIX big endian
PowerPC nt little endian
PowerPC non-nt big endian <----- PPC system is a large-end bytecode System
RS/6000 UNIX big endian
Linux UNIX big endian
IXP1200 ARM core all little endian

Related Article

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.