What the hell is a "byte order"?

Source: Internet
Author: User

What the hell is a "byte order"?

http://mp.weixin.qq.com/s?__biz=MjM5NTU2MTQwNA==&mid=2650652324&idx=1&sn= 5502d3ab977b9894f50592266544b30c&scene=0

Big-endian, small-ended thinking.
on the importance of order
1. Cooking the storyToday's girlfriend overtime, witty she had already prepared the ingredients last night, go home just to cook. Who knows the meeting is a bottomless pit, to the B1, there are B2, infinite chamber also.
As hard as she, in order to let her go home to eat hot meals, I am ready to cook, dedicate my first time. The ingredients are ready, I'm sure it's not that hard, reckoned should be the same process that I'm used to, firing | Add Ingredients | Top Ingredients | Stir Fry | Out of the pot, ah ha, think of a little excitement.
Today's dinner is tomato scrambled eggs and carrot fried meat, the actual operation only found, and met a big pit ...
The ingredients are:
Chopping Board No. 1th (ingredients for scrambled eggs with tomatoes), left-to-right, in turn: tomatoes, eggs, onionsChopping Board No. 2nd (Carrot Fried meat ingredients), left to right in turn: garlic, carrot silk, meat
The ingredients on the board uniform in turn, I put tomatoes first, or put eggs first, or first put the onion? After simple communication, I learned that the ingredients on the chopping board are put in order, I just need to order the pot. Listening to the TV humming the 90 's old songs, rinsed, two dishes scheduled to finish.
Smelling a strange smell, I knew it was the first time I had failed.
When she came home, after a review, she knew the order was misplaced. Each dish should be placed from right to left with ingredients, that is, onion-and egg-and tomato. Is this a tease for me? The average person understands that the default order should not be left to right! Friends, should I go from left to right or right to left?
2. The story of peeling eggs"Gulliver's Travels" in the record of the two great Powers, you would not think that they are fighting and the position of the egg-stripping.
Many people think that the egg should be peeled to break the larger end of the eggs, this group of people are called "Big endian pie." But the emperor's grandfather accidentally broke a finger when he was eating eggs. Therefore, his father (then the emperor) ordered to peel the eggs must break the small end of the egg, breach people heavy penalties, resulting in the "small End (Little endian) faction."

The people were extremely disgusted by the order, which led to 6 of rebellions, one of whom had been killed and the other lost his throne. It is estimated that several 11000 people would rather die than break the smaller end of the egg!
See no, just peel eggs can produce such a large divergence, "big-endian" and "small end" is so important!
byte order
1. BytesByte (byte) as a unit of measurement in the world of computers, and the number of yuan in the hands of the amount of "yuan" a meaning. Anyway, to the computer world, said the byte on the right, the use of People's basic unit of measurement, which is in the Romans.
For example, a movie is 1G bytes (1GB), a song is 10M bytes (10MB), and a picture is a 1K byte (1KB).
2. Byte orderWhat can I do for a dollar? Nothing can be done, the bus is not enough to sit. What can I do with a single byte? A minimum of one character can be saved.
When the data is too large and a byte cannot be saved, we have to use multiple bytes. For example, I have two integers that require 4 bytes of storage, and for illustrative purposes, use 16 notation for these two numbers, namely 0x12345678 and 0x11223344. Some people store this two numbers in the following ways:

Small end


This scenario looks good, but someone else has taken the following approach:

Big-endian high-low address saves space


Circle, in the end, which way to save! Although the two schemes are different, there is a consensus, that is, to store each number, that is, the first deposit 0x12345678, and then save 0x11223344. The difference is that for a certain value to be represented, because it can only be one byte in one byte, I save the value low to the lower address, or the high value to the low address. The former uses the "Small End (Little endian)" byte order, that is, the lower end of the first (two digits of the lowest bit is 0x78, 0x44), such as the first figure in, the latter using the "Big endian" byte sequence, That is, the end of the first high (the highest of the two digits is 0x12, 0x11), as in the second figure.
This also led to the computer world of the big and small end of the dispute, different CPU vendors have not reached a consensus:
X86,mos Technology 6502,z80,vax,pdp-11 and other processors are little endian.
· Motorola 6800,motorola 68000,powerpc 970,system/370,sparc (except for V9) processors such as big endian.
· ARM, PowerPC (except PowerPC 970), DEC Alpha, SPARC V9, MIPS, Pa-risc and IA64 the byte order is configurable.
Big end or small, right when it is a personal hobby, as long as you do not affect others on the line, right?
3. Network byte orderThe big and small ends of the front are talking about the computer itself, also known as the host byte order. In fact, as long as they can justify it is no problem. The problem is that the presence of the network makes it possible for the computer to communicate. Communication, it means to get along, get along must have common language ah, have to speak Putonghua, otherwise it would be easy to wrong, under the one-hour small movie found can't open, understand wrong!
But each computer has its own host byte-order Ah, still bricks, insist on doing their own, how to do?
TCP/IP protocol Grand appearance, RFC1700 the use of "big-endian" byte sequence for the network byte order, other computers do not use big-endian to note that when sending data must be the host byte order to the network byte order (i.e. "big-endian" byte order), the received data converted to their own host byte order. This is not related to the CPU, the operating system, to achieve the standardization of network communication. Suddenly feel, TCP/IP protocol is very capricious AH have wood!
For the sake of program compatibility, you will see that every time the programmer sends and accepts the data, the goal is to make sure that the code works as expected when it executes on any computer.
With such a common operation, the BSD socket provides a well-encapsulated conversion interface that is easy for programmers to use. Includes conversion functions from host byte order to network byte order: htons, htonl, conversion function from network byte order to host byte order: Ntohs, Ntohl. Of course, with the theoretical basis above, you can also write your own conversion function.
The following section of code can be used to determine whether the computer is big or small, the idea is to determine a multibyte value (using a 4-byte integer below), write it to memory (that is, assign to a variable), and then use the pointer to its first address of the corresponding byte (that is, a byte of low address), To determine whether the byte is high or low, the high note is big endian, low-level description is little endian.
#includeint main (){unsigned int x = 0x12345678;char *c = (char*) &x;if (*c = = 0x78) {printf ("Little endian");} else {printf ("Big endian");  }return 0;}
4. The byte order aroundCharacter encoding UTF-16, UTF-32 also face the problem of byte order, because they use 2 bytes and 4 byte encoding Unicode characters, once a value is expressed in more than one byte, it is necessary to consider the order of storage. So, in the simplest and most brutal way, write a few characters to the head of the file to indicate whether it is a big-endian or a small end:

Here have to mention UTF-8 Ah, clearly people are a single byte, there is no problem of what byte sequence. Microsoft in order to unify Utf-x, abruptly to his head also added a few characters! Yes, these characters are the BOM (Byte Order Mark), which is the UTF-8 under Windows.
Believe that many people have been UTF-8 BOM to the pit, more of this BOM UTF-8 file, will cause a lot of problems ah. For example, the shell script written, #!/usr/bin/env Bash, in the UTF-8 have BOM and UTF-8 without BOM code, the corresponding 16 binary is:

So, if there is a BOM, the Shell interpreter will error. The reason is that the interpreter wants to encounter #!/usr/bin/env bash, while the content that is encoded with the UTF-8 BOM will have a 3-byte EF BB BF.
For UTF-8 andUTF-8 No BOMTwo encoding formats, we use more UTF-8 without BOM.

Turn from:Bole Online

What the hell is a "byte order"?

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.