C # Small-end to large-end Little Endian to Big Endian,
What are big ends and small ends?
The definitions of Big-Endian and Little-Endian are as follows:
1) 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.
2) Big-Endian refers to the low address of high bytes discharged in the memory, and the low byte is discharged in the high address of the memory.
For example, the representation of the number 0x12 34 56 78 in the memory is as follows:
1) big end mode:
Low address ---------------> high address
0x12 | 0x34 | 0x56 | 0x78
2) Small-end mode:
Low address ----------------> high address
0x78 | 0x56 | 0x34 | 0x12
It can be seen that the large-end mode is similar to the string storage mode.
3) The following are two examples:
The 16-Bit Width 0x1234 is stored in the Little-endian mode (and Big-endian mode) CPU memory (assuming it starts from the address 0x4000) as follows:
Memory Address |
Content storage in Small-end Mode |
Content storage in big-end Mode |
Zero x 4000 |
0x34 |
0x12 |
Zero x 4001 |
0x12 |
0x34 |
32bit-width 0x12345678 in Little-endian mode and Big-endian mode:
Memory Address |
Content storage in Small-end Mode |
Content storage in big-end Mode |
Zero x 4000 |
0x78 |
0x12 |
Zero x 4001 |
0x56 |
0x34 |
Zero x 4002 |
0x34 |
0x56 |
Zero x 4003 |
0x12 |
0x78 |
Note that the following code test environment is VS2017 Professional Edition-C # language, Win7-64 bit-flagship version-SP1, this environment is the small-end mode, other environments are not guaranteed to be small-end Mode
Int x = 439041118; // The hexadecimal format is 1A2B3C5Estring s = null; byte [] B = BitConverter. getBytes (x); s = BitConverter. toString (B); // Console in Small-end mode. writeLine (s); // the output of the small end is 5E-3C-2B-1AArray.Reverse (B); // reverse s = BitConverter. toString (B); // Console in big-end mode. writeLine (s); // the output of the big end is 1A-2B-3C-5EConsole.ReadKey ();
Appendix:
C # determine the byte sequence of data stored in this computer structure ("Endian" type), that is, large or small
Int data = 439041118; // The hexadecimal format is 1A2B3C5Ebyte [] bData = BitConverter. getBytes (data); if (BitConverter. isLittleEndian) // if it is a small-end mode {Array. reverse (bData); // convert to the big-end mode} string s = BitConverter. toString (bData); Console. writeLine (s); Console. readKey ();