網絡字節順序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.