[Turn] big endian and small end sequence

Source: Internet
Author: User

PS: Big endian is readable (low address put high) (big-endian, Avenue to Jane) network end with a big-endian, such as sending a message, length + "Hello"

0x00, 0x00, 0x00,0x05, ' h ', ' e ', ' l ', ' l ', ' O '

What is big endian, small end order?

Simply put, it is the byte storage order, if the data are single-byte, then how to store it does not matter, but for multibyte data, such as int,double, it is necessary to consider the order of storage. Note that the byte order is a hardware-level thing that is usually transparent to software. Again white, the byte order is usually only related to the processor architecture you use, and not to the programming language, such as the common Intel x86 series is small-endian.

Big-endian (big endian)

The high-level byte of the data is stored at the low-end of the address.

Little-endian (small end order)

The high-level byte of the data is stored at the low end of the address.

BYTE High and low
For example, an int a = 0x12345678; Then the left 12 is the high byte, the right 78 is the low byte, from left to right, from high to low, (note that the height is relatively, for example, 56 is high relative to 78 bytes, relative to 34 is a low byte)

High-end and low-end addresses
0x00000001
0x00000002
0x00000003
0x00000004
From the top down, from low to high, the address value of small low-end, high-end address value.

How does a different byte order store data?

See how the data is stored in two ways, assuming that the hexadecimal number 0x12345678 starts at address 0x00000001, then

Bit-endian This storage (stored in the original order)
0x00000001--12
0x00000002--34
0x00000003--56
0X00000004--78

Little-endian so stored (reversed order storage)
0x00000001--78
0x00000002--56
0x00000003--34
0x00000004--12

A good memory method is that the big endian is stored in the order in which the numbers are written, while the small end order is stored in reverse order of writing.

Programming to determine big-endian and small-order

Method One

BOOL Isbigendian () {    int a =1 ;     if ((char*) &a) [3] = =1truefalse ;}         

Open the Memory window of VS and see how a is stored, at a glance

Since a is an int, it accounts for four bytes, and its value is 00000001, which is stored in the following way. So a[3] is 0, not the big endian. A more standard notation is to replace a[3] with a[sizeof (int)-1].

0x0012fe88 01

0x0012fe89 00

0x0012fe8a 00

0x0012fe8b 00

Method Two, the use of Union, the principle of the following surface questions.

BOOL Isbigendian () {    Union    {Short         A;        char b;} c; c.a =0x0102if (c.b = =1truefalse ;}      
A question of a face

To consolidate the problem, the following code output what?

union u{int I; char x[2 ";} A; int Main (void0 ] = ' 1  ; A.x[ 2 ' 

This question depends on what series of CPUs you are using, let's assume the Intel series. Union is a special structure in which all members share a memory address, and only one member can be stored at any time, and the Union size is 4 bytes, so the above code stores the characters 1 and 2, and the Union's storage looks like 0x31320000,31 and 32. Don't be hexadecimal ASCII characters ' 1 ' and ' 2 ' (note characters 1 and 2, not integers), but Intel series CPUs are stored in small-endian order, so the correct sequence is 0x00003231, and the corresponding decimal number is 12849, are you correct?

For more information on the byte order, see Wikipedia's introduction http://en.wikipedia.org/wiki/Endianness

= = Happy Coding = =

The End

[Turn] big endian and small end sequence

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.