Detailed explanation of byte order (go)

Source: Internet
Author: User

Original source: http://blog.csdn.net/yingfox/article/details/1831848

First, the basic concept

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

Bytes: A contiguous eight-bit is called a byte (byte), which is the smallest unit in the computer that can be processed separately. That is, in bytes
Explains the information for the basic unit, which specifies that 1 bytes is 8 bits.

Usually 1 ASCII codes are stored in 1 bytes, 1 characters are stored in 2 bytes, and double-precision floating-point real numbers are stored in 8 bytes.
The following list defines the bytes of the type and the range of data in the C language.


Type identifier type description Length (bytes) Range Comment

char character type 1-128 ~ 127-2^7~ (2^7-1)
unsigned char non-character 1 0 ~ 255 0~ (2^8-1)
short int shorter integer 2-32768 ~ 32767-2^15~ (2^15-1)
unsigned short int non-symbol 2 0 ~ 65535 0~ (2^16-1)
int integer 4-2147483648 ~ 2147483647-2^31~ (2^31-1)
unsigned int non-integral 4 0 ~ 4294967295 0~ (2^32-1)
float Real (single precision) 4 1.18*10-38 ~ 3.40*1038 7 bit valid bit
Double Real (double) 8 2.23*10-308 ~ 1.79*10308 15-bit significant bit
Long double solid (long dual precision) 10 3.37*10-4932 ~ 1.18*104932 19-bit significant bit


Second, byte order origin

When we write a stream, because the character type occupies only one byte, the computer only needs to press one character
Write the file. However, if the integer type is processed, because the integer is 4 bytes, an integral type internal
The order of byte storage arrangement is directly related to the integer value recognized by the computer. In a sense it can also be directly
Understanding the order in which computers are recognized is called byte order.


Iii. Some explanations of byte order

Different computer architectures sometimes store data in different byte order. For example, Intel-based computers store data
Is the opposite of the Macintosh (Motorola) computer. The Intel byte order is called "Little-endian",
On the other hand, the Macintosh (Motorola), and the use of the network standard is "Big-endian". When you apply an application from a rack
In the process of migrating to another schema type, you often encounter byte order (endianness) problems.
The byte order is the order in which the data element and its individual bytes are stored and represented in memory.

With the above analysis, there are two types of byte ordering: Big-endian (usually network byte order) and
Little-endian (host byte order).

Here are the nonsense explanations for these terms.

Big-endian the most important byte at the left end of the entire content.
Little-endian the most important byte at the right end of the entire content.
For Big-endian processors, when placing words in memory, starting with the lowest bit address, first put the most important
Bytes. On the other hand, for Little-endian processors, such as Intel processors, the first thing to put is the least important word
Section.

The concept of the above is still very vague, personally think that the people do not understand the byte order is absolutely nonsense, the internet is mostly
Copy these pieces of nonsense. Specifically, we illustrate this through practical examples.


Iv. Explanatory notes here

Let's look at the code below and we'll see what we've read.

This is the complete C-language code running under Hp-unix 9000/800, which is the Big-endian way.

1#include <unistd.h>2   voidMain ()3   {4   5   intI=0x41424344;6   7printf"int address:%x value:%x/n",&i,i);8printf"-------------------------------/N");9   Ten   Char* Paddress= (Char*) &i; One   intJ; A    -    for(j=0; j<=3; j + +) -   { theprintf"Char address:%x value:%c/n", paddress,*paddress); -paddress++; -   } -    +}



Compile output (cc-g ...) :

  int  address:7f7f08f0 value:41424344  -------------------------------  Char address:7f7f08f0 value:a  Char address:7f7f08f1 value:b  char address:7f7f08f2 value:c  Char address:7f7f08f3 value:d

Let's go back to Windows XP and look at the output of this code. Little-endian mode.

1#include <stdio.h>2   voidMain ()3   {4    intI=0x41424344;5printf"int address:%x value:%x/n",&i,i);6printf"-------------------------------/N");7    Char* Paddress= (Char*) &i;8    intJ;9     for(j=0; j<=3; j + +)Ten    { Oneprintf"Char address:%x value:%c/n", paddress,*paddress); Apaddress++; -    } -}

Compile 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:a



After reading the above code, it should be very clear, what byte order? What a simple death! int i=0x41424344;
With 16 binary, we know that A's acsii code is 65,16 is 41, understandable, this example is want to pass the output
A,b,c,d to verify the byte order. I'm going to make a list of the memory data and I believe it will be a deeper understanding.

The memory placement sequence of the Big-endian is as follows:

Address: 0x7f7f08f0 0x7f7f08f1 0x7f7f08f2 0x7f7f08f3

0x41 0x42 0x43 0x44

The memory placement sequence of the Little-endian is as follows:

Address: 0x0012ff7c 0x0012ff7d 0x0012ff7e 0x0012ff7f

0x44 0x43 0x42 0x41

V. Final NOTE

Host byte order (hosts)
Little-endian [Intel, VAX and Unisys processors, etc.]
NET byte order (network)
Big-endian [IBM 370, Motorola and most RISC designs----IBM mainframes and most UNIX platforms]

Byte conversions are mostly used in network programming, or in the case of code porting.
Some related functions in the UNIX environment: (Must include header file #include <netinet/in.h>)

Htons ()--"Host to Network short"
HTONL ()--"Host to Network Long"
Ntohs ()--"Network to Host short"
Ntohl ()--"Network to Host Long"

Some of the related functions of Windows. Net:

Hosttonetworkorder
Networktohostorder

Detailed explanation of byte order (go)

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.