Differences between big ends and small ends

Source: Internet
Author: User

1. What is byte order?

It refers to the order in which data is stored in the memory. Generally, a storage unit is 1 byte by default, and a logical data such as float is 4 bytes, the order of memory management storage can be high or low. in this way, there is a distinction.

2. What sequence are there:

Big-Endian (MSB), high at low address

Little-Endian (LSB), low at low address

Endian refers to the arrangement of logical to physical units when the minimum physical unit is smaller than the minimum logical unit hour.

3. Why is there a byte order?

Because the program needs to communicate with the outside world and transmit data, the outside world includes programs written in other machines and other languages. for example, the C ++ program under x86 communicates with the program under Linux, for example, the C ++ program communicates with the Java program. the storage order of bytes is involved.

4. Which are big-Endian and little-Endian?

The network protocols are all big-Endian, and Java compilation is all big-Endian.

Motorola's PowerPC is big-Endian's

The x86 series uses little-Endian to store data.

Arm supports both big and little. In practice, little-Endian is usually used.

The above content is reproduced from: http://www.cnblogs.com/kiddo/archive/2007/09/03/879941.html

 

Because the current computers are all stored in eight bytes, a 16-bit integer, that is, the short in C language, there can be two storage sequence in the memory: Big-Endian and litte-Endian. consider a short integer 0x3132 (0x32 is the low position, 0x31 is the high position) and assign it to a short variable. There may be two scenarios for storing it in the memory:

Big-Endian: ------------------- >>>>>>>> short variable address 0x1000 0x1001 _____________________________
|
| 0x31 | 0x32
| ________________ The high byte is before the low byte, that is, the high is at the end of the memory address. remember (large-end-> high-> front-> normal logical order) Small-end bytes (little-Endian) as follows ): ----------------- >>>>>>> memory address increase direction short variable address 0x1000 0x1001 _____________________________
|
| 0x32 | 0x31
| ________________
The low-level bytes are at the front of the High-level byte, that is, the low-level bytes are at the end of the low-level memory address. you can remember this (the opposite of the normal logic order between the small end and the low end and the front end). You can create an experiment in windows with the following Program # include <stdio. h> # include <assert. h> void main (void ){
Short test;
File * FP;

Test = 0x3132; // ('1' of 31asiic code, '2' of 32asiic Code) if (FP = fopen ("C: // test.txt ", "WB") = NULL) assert (0 );
Fwrite (& test, sizeof (short), 1, FP );
Fclose (FP);} Then open the test.txt file on the C drive. You can see that the content is 21, and test is equal to 0x3132. Obviously, we can see that the byte sequence of X86 is low. if we put the same code on the (big-Endian) machine for execution, the file is 12. this is no problem in the local machine. but when you copy this file from a big-endian machine to a little-endian machine, the problem occurs. as in the above example, we created the test file on the big-endian machine, copied it to the little-endian machine, and then read it into a short using fread, what we get is no longer 0x3132 but 0x3231, and the data we read is incorrect. Therefore, we need to be careful when transmitting data on two machines with different bytes, understanding the byte sequence can help us write more code. because of the differences in the byte sequence, big-Endian is used to define all data related to the byte sequence during network transmission. The BSD Code defines four macros for processing: # define ntohs (n) // network byte order to host byte order n Represents net, H represents host, s represents short # define htons (N) // host byte order to network byte order n Represents net, H represents host, s represents short # define ntohl (n) // network byte order to host byte order n Represents net, H stands for host, S stands for long # define htonl (n) // host byte order to network byte order N stands for net, H stands for host, S stands for the example of long to illustrate the implementation of one of these macros:
# Define sw16 (X )/
(Short )(/
(Short) (x) & (short) 0x00ffu) <8) |/
(Short) (x) & (short) 0xff00u)> 8) Here we implement an exchange of two bytes. the other macros are similar. let's rewrite the above Program # include <stdio. h> # include <assert. h> # define sw16 (X)/(short )(/
(Short) (x) & (short) 0x00ffu) <8) |/
(Short) (x) & (short) 0xff00u)> 8) // because x86 is low, switch to the network byte sequence # define htons (x) sw16 (x) void main (void ){
Short test;
File * FP;
Test = htons (0x3132); // ('1' of 31asiic code, '2' of 32asiic code ')
If (FP = fopen ("C: // test.txt", "WB") = NULL) assert (0 );
Fwrite (& test, sizeof (short), 1, FP );
Fclose (FP);} if the machine in front of the high byte is in the same sequence as the network byte, we can do nothing, just put # define htons (X) replace the sw16 (x) macro with # define htons (x ). when I understood this question at first, I was wondering why other data does not need to be exchanged in byte order? For example, when we write a buffer to a file, we finally want to understand it, because it is all written in unsigned char type, one byte and one byte. This order is fixed and there is no problem with the byte sequence, stupid enough ..

The above content is reproduced from: http://www.sf.org.cn/Article/base/200606/18679.html

 

In addition, Windows Sockets: byte sorting

Http://msdn.microsoft.com/zh-cn/library/cc468322 (V = vs.71). 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.