A detailed explanation of the byte order-about Big endian and Little endian

Source: Internet
Author: User

I. Basic Concepts

Bit concept: in a computer, a 0 or 1 is called a bit ).

Byte: the continuous eight bits are called one byte, And the byte is the minimum unit that can be processed separately by the computer. In bytes
It is the basic unit to explain the information. It specifies that one byte is eight binary bits.

Generally, one ASCII code is stored in one byte, one Chinese character is stored in two bytes, and the double-precision floating point real number is stored in eight bytes.
The following lists the bytes and data range of types defined in C language.


Type identifier type description length (byte) range remarks

Char character type 1-128 ~ 127-2 ^ 7 ~ (2 ^ 7-1)
Unsigned char unsigned character type 1 0 ~ 255 0 ~ (2 ^ 8-1)
Short int short integer 2-32768 ~ 32767-2 ^ 15 ~ (2 ^ 15-1)
Unsigned short int unsigned short integer 2 0 ~ 65535 0 ~ (2 ^ 16-1)
Int integer 4-2147483648 ~ 2147483647-2 ^ 31 ~ (2 ^ 31-1)
Unsigned int unsigned integer 4 0 ~ 4294967295 0 ~ (2 ^ 32-1)
Float solid (single precision) 4 1.18*10-38 ~ 3.40*1038 7-bit valid bits
Double real (double Precision) 8 2.23*10-308 ~ 1.79*10308 15-bit valid digits
Long double real type (long double Precision) 10 3.37*10-4932 ~ 1.18*104932 19-bit valid digits


Ii. Origins of byte order

When writing a compaction stream, because the number of bytes is only one character, the computer only needs to press one character and one character
Write the file. However, when processing an integer, because the integer occupies 4 bytes
The order of byte storage is directly related to the integer value recognized by the computer. In a sense, you can also directly
Understanding the computer's recognition order is the so-called byte order.


Iii. Explanations of byte order

Different computer structures sometimes use different bytes to store data. For example, Intel-based computers store data
The order is the opposite to that of a Macintosh computer. The Intel byte sequence is called "Little-Endian ",
On the contrary, the Macintosh (Motorola) adopts the "Big-Endian" standard on the network ". Before you change an application from
The endianness issue often occurs when the structure is migrated to another architecture type.
The order of bytes is the order in which data elements and their individual bytes are stored and expressed in the memory.

Through the above analysis, we can find two types of byte order: big-endian (usually network byte order) and
Little-endian (host byte sequence ).

The following is a nonsense explanation of these terms.

The most important byte of Big-Endian is on the left side of the entire content.
The most important byte of Little-Endian is on the right side of the entire content.
For the big-endian processor, when placing words in the memory, it starts from the memory bit address, first put the most important
Bytes. On the other hand, for a little-endian processor, such as an Intel processor, the first words are the least important.
Section.

The above concepts are still vague. I personally think that people who do not know the byte sequence are absolutely nonsense.
This is a copy of the nonsense. For details, see the actual example.


Iv. Explanations

Let's take a look at the following code and understand everything.

This is the complete C language code that runs in HP-UNIX 9000/800, that is, the Big-Endian method.

# Include <unistd. h>
Void main ()
{

Int I = 0x41424344;

Printf ("int Address: % x Value: % x/n", & I, I );
Printf ("-------------------------------/n ");

Char * pAddress = (char *) & I;
Int j;

For (j = 0; j <= 3; j ++)
{
Printf ("char Address: % x Value: % c/n", pAddress, * pAddress );
PAddress ++;
}

}

Compile the output (cc-g ...):

Int Address: 7f7f08f0 Value: 41424344
-------------------------------
Char Address: 7f7f08f0 Value:
Char Address: 7f7f08f1 Value: B
Char Address: 7f7f08f2 Value: C
Char Address: 7f7f08f3 Value: D

Go back to Windows XP and check the output of this Code. Little-Endian mode.

# Include <stdio. h>
Void main ()
{
Int I = 0x41424344;
Printf ("int Address: % x Value: % x/n", & I, I );
Printf ("-------------------------------/n ");
Char * pAddress = (char *) & I;
Int j;
For (j = 0; j <= 3; j ++)
{
Printf ("char Address: % x Value: % c/n", pAddress, * pAddress );
PAddress ++;
}
}

Compile the output (VC 6.0 ):

Int Address: 12ff7c Value: 41424344
-------------------------------
Char Address: 12ff7c Value: D
Char Address: 12ff7d Value: C
Char Address: 12ff7e Value: B
Char Address: 12ff7f Value:

After reading the above Code, we should be very clear about the byte sequence? It's really easy! Int I = 0x41424344;
In hexadecimal notation, we know that the acⅱ code of A is 65, and the hexadecimal notation is 41. In this example, we want to output
A, B, C, and D are used to verify the byte sequence. I will list the memory data, and I believe I will have a deeper understanding.

The memory placement sequence of Big-Endian is as follows:

Address: 0x7f7f08f0 0x7f7f08f1 0x7f7f08f2 0x7f7f08f3

0x41 0x42 0x43 0x44

The memory placement sequence of Little-Endian is as follows:

Address: 0x0012ff7c 0x0012ff7d 0x0012ff7e 0x0012ff7f

0x44 0x43 0x42 0x41

V. Final description

Host byte sequence (Host)
Little-Endian [Intel, VAX, and Unisys processors]
Network)
Big-Endian [IBM 370, Motorola, and most of the RISC designs-IBM mainframe and most Unix platforms]

Byte conversion is mostly used in network programming or code porting.
Related functions in Unix: (the header file must be included # include <netinet/in. h>)

Htons () -- "Host to Network Short"
Htonl () -- "Host to Network Long"
Ntohs () -- "Network to Host Short"
Ntohl () -- "Network to Host Long"

Windows. Net related functions:

HostToNetworkOrder
NetworkToHostOrder

Note: Many people repost this article, so I don't know who is the original. As a thank you, I can only put the Blog address I have read to repost this article here:
Http://blog.csdn.net/yingfox/archive/2007/10/18/1831848.aspx

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.