Byte order problems

Source: Internet
Author: User

I. Definition of byte order
The byte order, as the name implies, is the order in which data of a byte type is stored in the memory. (Of course, there is no need to talk about the order of data of a byte ).

In fact, most people rarely deal with the byte sequence directly in actual development. Only cross-platform and network programs should be considered in the byte sequence.

In all the articles about the byte sequence, we mentioned that the byte sequence is divided into two types: Big-Endian and little-Endian. The definitions of reference standard Big-Endian and little-Endian are as follows:
A) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.
B) Big-Endian refers to the low address of the memory where the high byte is discharged, and the low byte is discharged to the high address of the memory.
C) network byte sequence: TCP/IP Protocols define the byte sequence as big-Endian. Therefore, the byte sequence used in TCP/IP is usually called the network byte sequence.

In fact, I was confused when I first saw this definition. After reading a few examples, I was also very confused. What high/low address end? What is the level? I learned a little bit after reading some documents.

Ii. high/low addresses and high/low bytes
First, we need to know the memory space layout in our C program image: the memory space layout is described in "C expert programming" or "Unix environment advanced programming, for example:
----------------------- Maximum memory address 0 xffffffff
| Stack bottom
.
Stack
.
Stack top
-----------------------
|
|
/|/

NULL (empty)

/|/
|
|
-----------------------
Heap
-----------------------
Uninitialized data
---------------- (Collectively referred to as data segment)
Initialized data
-----------------------
Body section (code segment)
----------------------- Minimum Memory Address 0x00000000

For example, if we allocate an unsigned char Buf [4] on the stack, how do we arrange this array variable on the stack? [NOTE 1] See:
Stack bottom (high address)
----------
Buf [3]
Buf [2]
Buf [1]
Buf [0]
----------
Stack top (low address)

Now we have figured out the high and low addresses, and then I will find out the high/low bytes. If we have a 32-bit unsigned integer 0x12345678 (haha, the above four-byte Buf is regarded as an integer. So what is the high position and what is the low position? It is actually very simple. In decimal, we all say that the value on the left is high, and the value on the right is low. This is also true in other hexadecimal formats. Take 0x12345678 as an example. The bytes from high to low are 0x12, 0x34, 0x56, and 0x78.

Both the High and Low addresses and the high and low bytes are clarified. Let's review the definitions of big-Endian and little-Endian, and illustrate the two types of byte order with the illustration:
Taking unsigned int value = 0x12345678 as an example, we can use unsigned char Buf [4] to show the storage conditions of the two types of bytes respectively:
Big-Endian: high storage for low addresses, for example:
Stack bottom (high address)
---------------
Buf [3] (0x78) -- low
Buf [2] (0x56)
Buf [1] (0x34)
Buf [0] (0x12) -- high
---------------
Stack top (low address)

Little-Endian: Low-address storage, such:
Stack bottom (high address)
---------------
Buf [3] (0x12) -- high
Buf [2] (0x34)
Buf [1] (0x56)
Buf [0] (0x78) -- low
---------------
Stack top (low address)

On the existing platform, intel X86 uses little-Endian, while Sun's iSCSI uses big-Endian.

Iii. Example
Test Platform: Sun iSCSI Solaris 9 and intel X86 Solaris 9
In our example, the same program is used to read the content of the same binary file on a platform with different bytecode.
The Program for generating binary files is as follows:
/* Gen_binary.c */
Int main (){
File * fp = NULL;
Int value = 0x12345678;
Int Rv = 0;

Fp = fopen ("Temp. dat", "WB ");
If (FP = NULL ){
Printf ("fopen error/N ");
Return-1;
}

Rv = fwrite (& Value, sizeof (value), 1, FP );
If (RV! = 1 ){
Printf ("fwrite error/N ");
Return-1;
}

Fclose (FP );
Return 0;
}

The Program for reading binary files is as follows:
Int main (){
Int value = 0;
File * fp = NULL;
Int Rv = 0;
Unsigned char Buf [4];

Fp = fopen ("Temp. dat", "rb ");
If (FP = NULL ){
Printf ("fopen error/N ");
Return-1;
}

Rv = fread (BUF, sizeof (unsigned char), 4, FP );
If (RV! = 4 ){
Printf ("fread error/N ");
Return-1;
}

Memcpy (& Value, Buf, 4); // or value = * (int *) BUF );
Printf ("the value is % x/N", value );

Fclose (FP );
Return 0;
}

Test process:
(1) generate the temp. dat file on the Linux platform
The result of reading the temp. dat file on the Linux platform:
The value is 12345678

The result of reading the temp. dat file on the x86 Platform:
The value is 78563412

(1) generate the temp. dat file on the x86 Platform
The result of reading the temp. dat file on the Linux platform:
The value is 78563412

The result of reading the temp. dat file on the x86 Platform:
The value is 12345678

[NOTE 1]
Buf [4] layout on the stack is also obtained through the example program:
Int main (){
Unsigned char Buf [4];

Printf ("The Buf [0] ADDR is % x/N", Buf );
Printf ("The Buf [1] ADDR is % x/N", & Buf [1]);

Return 0;
}
Output:
The following table lists the Supported tools:
The Buf [0] ADDR is ffbff788
The Buf [1] ADDR is ffbff789
X86 Platform:
The Buf [0] ADDR is 8047ae4
The Buf [1] ADDR is 8047ae5

The addresses of both platforms are Buf [X] and higher than Buf [y] (x> Y ).

 

 

20090824 supplement:

1. Network and host byte Conversion Function: htons ntohs htonl ntohl (s means that short L is long H is host N is network)
2. different operating systems run on different CPUs in different bytes. See the following table.
Processor operating system byte sorting
Alpha all little endian
HP-PA nt little endian
UNIX big endian HP-PA
Intelx86 all little endian <----- x86 systems are small-end bytecode Systems
Motorola680x () All big endian
MIPs nt little endian
MIPs UNIX big endian
PowerPC nt little endian
PowerPC non-nt big endian <----- PPC system is a large-end bytecode System
RS/6000 UNIX big endian
Linux UNIX big endian
IXP1200 ARM core all little endian

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.