Big-end mode and small-terminal mode, network byte order and host byte order (classic)

Source: Internet
Author: User
Tags string format htons

http://wxxweb.blog.163.com/blog/static/135126900201022133740759/

big end mode and small terminal mode
First, the concept and detailed explanation
There are two main types of byte storage mechanisms used in computers of various systems: Big-endian and Little-endian, namely big-end mode and small-terminal mode.
Review two keywords first, MSB and LSB:
Msb:most significant bit-------most significant bits
Lsb:least significant bit-------least significant bits
Big-endian mode (Big-edian)
The BIG-ENDIAN:MSB is stored at the lowest end of the address.
For example, a double-byte number 0x1234 in a Big-endian way in the starting address 0x00002000:
| Data |<--Address
| 0x12 |<--0x00002000
| 0x34 |<--0x00002001
In Big-endian, the ordinal arrangement for a bit sequence is as follows (in the case of a double-byte number 0x8b8a):
bit | 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15
------MSB----------------------------------LSB
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+--------------------------------------------+
= 0x8 B 8 A
Small end mode (Little-endian)
The LITTLE-ENDIAN:LSB is stored at the lowest end of the address.
For example, a double-byte number 0x1234 in a Little-endian way in the starting address 0x00002000:
| Data |<--Address
| 0x34 |<--0x00002000
| 0x12 |<--0x00002001
In Little-endian, the ordinal choreography and Big-endian in a bit sequence are just the opposite, as in the case of a double byte number 0x8b8a:
bit | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0
------MSB-----------------------------------LSB
Val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+---------------------------------------------+
= 0x8 B 8 A
Second, the array in the small end of the case of storage:
Take the unsigned int value = 0x12345678 as an example, to see the storage situation in both byte order, we can use unsigned char buf[4] to represent value:
Big-endian: Low address storage high, as follows:
High Address
---------------
BUF[3] (0x78)--Low
BUF[2] (0x56)
BUF[1] (0x34)
Buf[0] (0x12)--high
---------------
Low Address
Little-endian: Lower address storage low, as follows:
High Address
---------------
BUF[3] (0x12)--high
BUF[2] (0x34)
BUF[1] (0x56)
Buf[0] (0x78)--Low
--------------
Low Address

Three big-end small-ended conversion method:
Big-endian converted into Little-endian as follows:
#define BIGTOLITTLE16 (A) (((UInt16) (a) & 0xff00) >> 8 | \
(((UInt16) (A) & 0X00FF) << 8))
#define BIGTOLITTLE32 (A) (((UInt32) (a) & 0xff000000) >> 24 | \
(((UInt32) (A) & 0x00ff0000) >> 8) | \
(((UInt32) (A) & 0x0000ff00) << 8) | \
(((UInt32) (A) & 0x000000ff) << 24))

Four big-end small terminal detection method:
How do I check that the processor is Big-endian or Little-endian?
Union union is stored in the order that all members are stored from a low address, using this feature can easily get CPU on memory using Little-endian or Big-endian mode read and write.
int Checkcpuendian ()
{
Union
{
unsigned int A;
unsigned char b;
}c;
C.A = 1;
return (C.B = 1);
}
/*return 1:little-endian, return 0:big-endian*/

Network byte order

1, the byte within the bit is not affected by this order
such as a byte 1000 0000 (or hexadecimal 80H), regardless of the order in which it is represented in memory.
2, more than 1 bytes of data types have byte order problem
For example, byte A, this variable has only one byte length, so there is no byte order problem based on the previous one. So the byte order is the meaning of "relative order between bytes."
3, the byte order of a data type greater than 1 bytes has two kinds
For example, short B, which is a two-byte data type, then there is the question of the relative order between bytes.
The network byte order is the order of WYSIWYG. The Intel type of CPU has the opposite byte order.
For example, the short b=0102h (16 binary, each two digits represents the width of a byte). What is seen is "0102", according to General mathematical knowledge, the axis from left to right to increase the direction, that is, the memory address from left to right increase, in memory this short B byte order is:
01 02
This is the network byte order. The order you see is the same as the order in memory.
The reverse byte order is different, in memory in the order of: 02 01
Suppose that the two bytes of network data obtained by grasping the packet are: 01 02
If this represents a variable of two byte types, then naturally there is no need to consider the problem of byte order.
If this represents a short variable, then you need to consider the byte order problem. According to the "WYSIWYG" rule of network byte order, the value of this variable is: 0102
Assuming the local host is an Intel type, it's a bit cumbersome to represent this variable:
Define variable short X,
The byte stream address is: PT, in order to read the memory is
x=* ((short*) PT);
So the memory order of X is 01 02, of course.
According to the "WYSIWYG" rule, this memory sequence is clearly not the same as the one you see, so swap the two byte positions.
The replacement method can be defined by itself, but it is more convenient to use an existing API.

Network byte order and host byte order
Nbo and HBO network byte ordering Nbo (network byte order): storage in order from highest to lowest, using a uniform network byte order on the network to avoid compatibility issues. Host byte order: Different machine HBO is not the same, and CPU design related to computer data storage has two byte precedence: High byte priority and low byte priority. Data on the Internet is transmitted over the network in high byte order, so the conversion is needed to transfer data over the Internet for machines that store data internally in low byte priority.

Htonl ()
Briefly:
Converts the unsigned long integer number of hosts to network byte order.
#include <winsock.h>
U_long PASCAL FAR htonl (U_long hostlong);
Hostlong: 32 Digits of host byte order expression.
Comments:
This function converts a 32-digit number from host byte order to network byte order.
return value:
HTONL () returns the value of a network byte order.

Inet_ntoa ()
Briefly:
Converts the network address to "." The dotted string format.
#include <winsock.h>
Char far* PASCAL FAR inet_ntoa (struct in_addr in);
In: A structure that represents the address of an Internet host.
Comments:
This function converts an Internet address structure represented by the in parameter to a string form such as "a.b.c.d" with the "." Interval. Note that the string returned by Inet_ntoa () is stored in the memory allocated by the Windows sleeve interface implementation. The application should not assume that the memory is allocated. Data is guaranteed to be valid until the next Windows socket interface is invoked on the same thread.
return value:
If no error occurs, Inet_ntoa () returns a character pointer. Otherwise, NULL is returned. The data should be copied before the next Windows socket interface is invoked.

The data transmitted in the network is consistent with the local byte storage order, while others are very different, for the consistency of the data, the local data must be converted into the format used on the network, then sent out, receive the same, after the conversion and then to use the data, The basic library functions provide such functions as byte conversion, such as and Htons () htonl () Ntohs () Ntohl (), where n indicates that network,h () host,htons () is used to convert local bytes to network bytes. s represents short, that is, 2-byte operations, L represents long, and 4-byte operations. similarly ntohs () Ntohl () is used to convert network bytes to local format.

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.