Determine the byte order in. NET under Windows systems

Source: Internet
Author: User

  byte order, which is the order in which bytes are stored in memory. It is also divided into the big endian byte (Big-endian) sequence and the small End byte order (Little-endian).

The following excerpt from Baidu Encyclopedia:

A) The Little-endian is the low-bit bytes emitted at the lower address of the memory, high-bit bytes emitted in the memory of the higher address.

b) The Big-endian is the high-bit byte emitted at the low address of the memory, and the low byte is discharged at the upper address of the memory.

What do you mean?

We have a int32 value: 16909060, its binary representation is: 0000 0001,0000 0010,0000 0011,0000 0100 (i added ', ' for easy Viewing)

Well, in memory we need 4 bytes to hold this integer value. Assume that the required memory address is: 0x00000001,0x00000002,0x00000003,0x00000004

For a small-endian byte sequence, it is stored in the following order:

0x00000001 0x00000002 0x00000003 0x00000004
0000 0100 0000 0011 0000 0010 0000 0001

For big-endian byte order, it is stored in the following order:

0x00000001 0x00000002 0x00000003 0x00000004
0000 0001 0000 0010 0000 0011 0000 1000

So, what kind of byte order is in. NET?

The first conclusion is that. NET uses Little-endian in memory.

Test principle: Use the Buffer.blockcopy method to copy the value of a int32 bit into the Int16 value. The Buffer.blockcopy method copies the specified count array from src to DST, and can specify the offset at which replication begins. The BlockCopy method accesses the bytes in the src parameter array using the memory offset, rather than using a programmatic construct such as an index or a lower bound on the array. Note that it is the offset of the memory, that is, the method is a copy of the value from a single byte in memory, rather than copying the numeric structure directly

Here is the source code:

1           varsours =New int[] {175 };2             varDest =New  Short[1];3             varSP =0;//source sequence offset address4             varDP =0;//Destination location offset address5             varCount =2;//number of bytes copied at a time6 7 buffer.blockcopy (sours, SP, dest, DP, count);8             foreach(varIinchdest)9             {Ten Console.WriteLine (i); One}

Result output: 175.

The code replicates 2 bytes at a time, and the good is the byte size required for a int16. The starting offset of the source array is 0. If we change the SP value to 2, which is 2 bytes, the final output becomes 0. This is because the high byte of 175 is 0000 0000,0000 0000, which is stored in the high address of the memory.

Determine the byte order in. NET under Windows systems

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.