BYTE order problem--size end problem

Source: Internet
Author: User
byte order problem--The small End method

Turn from: http://blog.csdn.net/sunjiajiang/article/details/7163338
Category: ARM 2011-12-29 21:21 2003 people read Comments (2) Collection report

Definition of a byte order

byte order, as the name implies byte order, say two more words is greater than a byte type of data in the memory of the order (a byte of the data of course there is no need to talk about the order of the problem).

In fact, most people rarely deal directly with byte-order in actual development. Only the byte-order in Cross-platform and network programs is a problem that should be considered.

In all of the articles that introduce the byte sequence, the byte order is divided into two categories: Big-endian and Little-endian. References to standard Big-endian and Little-endian are defined as follows:
A) Little-endian is the low byte emissions in the memory of the lower address end, high byte emissions at the high address of memory.
b Big-endian is the high byte emissions in the memory of the low address end, low byte emissions in the memory of the higher address.
c) Network byte order: the 4 byte bit value is transmitted in the following order: First is 0~7bit, second 8~15bit, then 16~23bit, and finally 24~31bit. This transmission order is called a big endian byte sequence. Because all binary integers in the TCP/IP header are required in this order when they are transmitted across the network, it is also known as network byte order. For example, a 2-byte "Ethernet frame Type" in the Ethernet header indicates the type of data that follows. For the Ethernet frame type of the ARP request or answer, the order in which the network is transmitted is 0x08,0x06. The image in memory is shown in the following illustration:
Bottom of stack (high address)
---------------
0X06--Low
0X08--High
---------------
Top of stack (low address)
The value of this field is 0x0806. stored in memory in the big way.

Two, high/low address and high-low byte

First we need to know our C program image in the space layout of memory: In the "c expert programming" or "UNIX environment Advanced Programming" in the memory space layout of the description, roughly the following figure:
-----------------------Maximum memory address 0xFFFFFFFF
| Bottom of Stack
.
. Stack
.
Top of Stack
-----------------------
|
|
\|/

NULL (empty)

/|\
|
|
-----------------------
Heap
-----------------------
Uninitialized data
----------------(collectively, data segment)
Initialized data
-----------------------
Body section (Code snippet)
-----------------------Minimum memory address 0x00000000

For example, if we assign a unsigned char buf[4 on the stack, how does the array variable lay on the stack [note 1]. Look at the picture below:
Bottom of stack (high address)
----------
BUF[3]
BUF[2]
BUF[1]
BUF[0]
----------
Top of stack (low address)

Now that we've figured out the high and low byte, then we're going to figure out if we have a 32-bit unsigned integer 0x12345678 (oh, just to see the 4 bytes above buf as an integer), what's the highs and lows? It's actually very simple. In the decimal we all say that the left side is high, the right side is low, in other systems as well. Take 0x12345678, the bytes from high to low are 0x12, 0x34, 0x56, and 0x78 in turn.

The high and low addresses and the high and low bytes are clear. Let's review the definitions of Big-endian and Little-endian and illustrate the two byte sequences graphically:
Take the unsigned int value = 0x12345678 as an example, to see the storage situation in both byte order, we can use unsigned char buf[4] to represent value:
Big-endian: Low address storage high, as shown below:
Bottom of stack (high address)
---------------
BUF[3] (0x78)--Low
BUF[2] (0x56)
BUF[1] (0x34)
Buf[0] (0x12)--high
---------------
Top of stack (low address)

Little-endian: Lower address storage low, as shown below:
Bottom of stack (high address)
---------------
BUF[3] (0x12)--high
BUF[2] (0x34)
BUF[1] (0x56)
Buf[0] (0x78)--Low
---------------
Top of stack (low address)

Intel's X86 on the existing platform is Little-endian, and the sun-like SPARC uses Big-endian.

Iii. examples

Embedded system developers should have a good understanding of Little-endian and Big-endian patterns. The CPU in Little-endian mode stores the operands from low byte to high byte, whereas the Big-endian mode stores the operands from high byte to low byte.

For example, the 16bit-wide number of 0x1234 stored in Little-endian mode CPU memory (assuming the address 0x4000 starts with) is:

Memory Address Store Content
0x4001 0x12
0x4000 0x34

In Big-endian mode CPU memory is stored in the following way:

Memory Address Store Content
0x4001 0x34
0x4000 0x12

The 32bit-wide number of 0x12345678 in Little-endian mode CPU memory (assuming starting from address 0x4000) is:

Memory Address Store Content
0x4003 0x12
0x4002 0x34
0x4001 0x56
0x4000 0x78

In Big-endian mode CPU memory is stored in the following way:

Memory Address Store Content
0x4003 0x78
0x4002 0x56
0x4001 0x34
0x4000 0x12

big-End method, small tip method, network byte order turn

On the definition of byte-order (big-end method, small-ended method)

Unxi Network Programming Definition: The term "small" and "big End" indicate which end of a multibyte value (small or big end) is stored at the starting address of the value. A small end has a starting address, that is, a small-endian byte sequence; a big-end exists starting address, which is a big-endian byte sequence.

can also say:
1. The small-end method (Little-endian) is the low byte emissions in the memory of the lower address is the starting address of the value, high byte emissions in the memory of the high address end.
2. Big-End Method (Big-endian) is the high byte emissions in the memory of the low address, that is, the value of the starting address, low byte emissions in the high address end of memory.

To give a simple example, for plastic 0x12345678. It is stored as shown in Figure 1 in the system of the big end method and the small tip method.

Network byte order

We know that the data flow on the network is a byte stream, for a multi-byte value, in the network transmission, the first pass which bytes. That is, when the receiving end receives the first byte, it handles the byte as a high or low.
Network byte order definition: The first byte received is treated as high, which requires that the first byte sent by the sender should be high. When sending data at the sender, the first byte sent is the byte corresponding to the starting address of the number in memory. Visible multi-byte values in memory should be stored in the big-end method before sending.
The network byte order is a big-endian byte sequence.
For example, we pass the network to send 0x12345678 this plastic, in the 80x86 platform, it is stored in a small-end method, before sending need to use the system provided by the HTONL to store it into a big way, as shown in Figure 2.

BYTE sequence test program

The byte sequence on different CPU platforms is usually not the same, and here is a simple C program that tests the byte order on different platforms.

1

#include <stdio.h>

2

#include <netinet/in.h>

3

int main ()

4

{

5

int i_num = 0x12345678;

6

printf ("[0]:0x%x\n", * ((char *) &i_num + 0));

7

printf ("[1]:0x%x\n", * ((char *) &i_num + 1));

8

printf ("[2]:0x%x\n", * ((char *) &i_num + 2));

9

printf ("[3]:0x%x\n", * ((char *) &i_num + 3));

10

11

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.