Byte order big-Endian & Little-Endian

Source: Internet
Author: User
Byte order big-Endian & Little-Endian2007-06-19

1. What is byte order?

It refers to the order in which data is stored in the memory. Generally, a storage unit is 1 byte by default, and a logical data such as float is 4 bytes, the order of memory management storage can be high or low. in this way, there is a distinction.

2. What sequence are there:

Big-Endian (MSB), high at low address

Little-Endian (LSB), low at low address

Endian refers to the arrangement of logical to physical units when the minimum physical unit is smaller than the minimum logical unit hour.

3. Why is there a byte order?

Because the program needs to communicate with the outside world and transmit data, the outside world includes programs written in other machines and other languages. for example, the C ++ program under x86 communicates with the program under Linux, for example, the C ++ program communicates with the Java program. the storage order of bytes is involved.

3. Which are big-Endian and little-Endian?

The network protocols are all big-Endian, and Java compilation is all big-Endian.

Motorola's PowerPC is big-Endian's

The x86 series uses little-Endian to store data.

Arm supports both big and little. In practice, little-Endian is usually used.

4. How can I determine the byte storage sequence used in my current system?

/* C code used to determine whether the storage format is little endian or big ednian */
# Include "stdafx. H"

Using namespace STD;

Union
{
Long long;
Char char [sizeof (long)];
} U;

Void main ()
{
U. Long = 1;
If (U. Char [0] = 1)
{

Printf ("little endian! "N ");

}
Else if (U. Char [sizeof (long)-1] = 1)
{
Printf ("big
Endian! "N ");
}
Else
{
Printf ("unknown
Addressing! "N ");
}

Printf ("now, let's look at every byte in the memory! "N ");


For (INT I = 0; I <sizeof (long); ++ I)
{

Printf ("[% x] = % x" N ", & U. Char [I], U. Char [I]);
}

Getchar ();
Return;
}

5. How to convert the byte sequence? Big-Endian --> little-Endian or little endian-> big endian.

# Include <stdio. h>
Const unsigned char size_of_unsignedint =
Sizeof (unsigned INT );
Const unsigned char size_of_unsignedchar =
Sizeof (unsigned char );

Void put_32 (unsigned char * cmd, unsigned int data)
{
Int I;

For (I = size_of_unsignedint-1; I> = 0; -- I)
{
CMD [I] =
Data % 256;
// Or you can:
// Cmd [I] = Data & 0xff;

Data = data> 8;
}
}

Unsigned int get_32 (unsigned
Char * cmd)
{
Unsigned int ret;
Int I;
For (ret = 0, I
= Size_of_unsignedint-1; I> = 0; -- I)
{
Ret = RET
<8;
RET | = cmd [I];
}
Return ret;
}

Int main (void)
{
Unsigned char cmd [size_of_unsignedint];

Unsigned int data, RET;
Unsigned char * P;
Int I;
Data =
0x12345678;
Printf ("Data = % x" N ", data );
// Print data in bytes

P = (unsigned char *) (& data );
For (I = 0; I <
Size_of_unsignedint; ++ I)
{
Printf ("% x", * P ++ );
}

Printf ("N ");
// Store the data in the CMD in the reverse order.
Put_32 (CMD,
Data );
For (I = 0; I <size_of_unsignedint; ++ I)
{

Printf ("cmd [% d] = % x" N ", I, CMD [I]);
}
//
Save the data to ret in reverse order.
// The stored RET value should be the same as the data value
Ret = get_32 (CMD );

Printf ("ret = % x" N ", RET );
P = (unsigned char *) (& RET );

For (I = 0; I <size_of_unsignedint; ++ I)
{
Printf ("% x ",*
P ++ );
}
Printf ("N ");
Getchar ();
Return 0
;
}

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.