大小端以及字节序的问题

网络字节顺序NBO(Network Byte Order):按从高到低的顺序存储,在网络上使用统一的网络字节顺序,可以避免兼容性问题。

The order in which the bytes of a multi-byte number are transmitted on a network - most significant byte first (as in "big-endian" storage). 

主机字节顺序(HBO,Host Byte Order):不同的机器HBO不相同,与CPU设计有关计算机数据存储有两种字节优先顺序:高位字节优先和低位字节优先。

 C#中可以通过BitConverter.IsLittleEndian

Console.WriteLine("BitConverter.IsLittleEndian = {0}", BitConverter.IsLittleEndian); 

http://blog.sina.com.cn/s/blog_6a6a80aa01013530.html

http://blog.sina.com.cn/s/blog_6a6a80aa0101352n.html

需要注意的是无符号的整型

uint a=3758205120;  

从下图可以看出最高位的字节是 1110 0000 是没有符号位的

namespace System.Net

public class IPAddress

{

public static int HostToNetworkOrder(int host);

public static long HostToNetworkOrder(long host);

public static short HostToNetworkOrder(short host);

}

IPAddress中处理的都是有符号的

int d=2147483647;  //2^31-1

//d=2147483648;  //int的取值不能超出2147483647,这个赋值溢出了

但是uint就可以赋值:  uint d = 2147483648;

===2015年09月23日更新===

字节数组的高低位问题

 Console.WriteLine("BitConverter.IsLittleEndian = {0}", BitConverter.IsLittleEndian);
                const int number = 0x01020304;
                Console.WriteLine("0x01020304 = {0}", number);

                var array = BitConverter.GetBytes(number);
                int index = -1;
                foreach (byte b in array)
                {
                    index++;
                    Console.WriteLine("array[{0}] = 0x{1}", index, b.ToString("x2"));
                }

输出结果是:

BitConverter.IsLittleEndian = True
0x01020304 = 16909060
array[0] = 0x04
array[1] = 0x03
array[2] = 0x02
array[3] = 0x01

0x01020304对应4字节的数字16909060,二进制为  0000 0001,0000 0010,0000 0011,0000 0100

4个字节从高到低: 最高位是0000 0001   最低位是0000 0100

BitConverter.GetBytes(number);

 经过BitConverter.GetBytes获取的字节数组,下标小的对应于最低位

BitConverter VS ToString for Hex

 Console.WriteLine("BitConverter.IsLittleEndian = {0}", BitConverter.IsLittleEndian);
                var result = int.MaxValue.ToString("X");
                Console.WriteLine(result);//Result: 7FFFFFFF
                result = BitConverter.ToString(BitConverter.GetBytes(int.MaxValue));
                Console.WriteLine(result); //Result: FF-FF-FF-7F

输出结果:

BitConverter.IsLittleEndian = True
7FFFFFFF
FF-FF-FF-7F

解答:

int.MaxValue.ToString("X") outputs 7FFFFFFF, that is, the number 2147483647 as a whole.

On the other hand, BitConverter.GetBytes returns an array of bytes representing 2147483647 in memory. On your machine, this number is stored in little-endian (highest byte last). And BitConverter.ToString operates separately on each byte, therefore not reordering output to give the same as above, thus preserving the memory order.

However the two values are the same : 7F-FF-FF-FF for int.MaxValue, in big-endian, and FF-FF-FF-7F for BitConverter, in little-endian. Same number.


字节序的高低位
高位和低位,参看计算器-->程序员,左高右低
比如0x0102 两个字节 01是高位,02是低位
小端字节序:高位在高地址,低位在低地址
大端字节序:高位在低地址,低位在高地址

192.168.1.224
小端字节序:0xc0a801e0 对应数字3232236000
大端字节序:0xe001a8c0   假如按照小端来解析数字的话是3758205120,按照大端来解析首先反转,然后再解析会是3232236000

int.MaxValue.ToString("X") outputs 7FFFFFFF, that is, the number 2147483647 as a whole.

On the other hand, BitConverter.GetBytes returns an array of bytes representing 2147483647 in memory. On your machine, this number is stored in little-endian (highest byte last). And BitConverter.ToString operates separately on each byte, therefore not reordering output to give the same as above, thus preserving the memory order.

However the two values are the same : 7F-FF-FF-FF for int.MaxValue, in big-endian, and FF-FF-FF-7F for BitConverter, in little-endian. Same number.

原文地址:https://www.cnblogs.com/chucklu/p/4818319.html