大小端以及字节序的问题
2015-09-18 09:44
162 查看
网络字节顺序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
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日更新===
字节数组的高低位问题
输出结果是:
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获取的字节数组,下标小的对应于最低位
http://stackoverflow.com/questions/6107121/bitconverter-vs-tostring-for-hex
输出结果:
BitConverter.IsLittleEndian = True
7FFFFFFF
FF-FF-FF-7F
解答:
On the other hand,
However the two values are the same :
字节序的高低位
高位和低位,参看计算器程序员,左高右低
比如0x0102 两个字节 01是高位,02是低位
小端字节序:高位在高地址,低位在低地址
大端字节序:高位在低地址,低位在高地址
192.168.1.224
小端字节序:0xc0a801e0 对应数字3232236000
大端字节序:0xe001a8c0 假如按照小端来解析数字的话是3758205120,按照大端来解析首先反转,然后再解析会是3232236000
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获取的字节数组,下标小的对应于最低位
http://stackoverflow.com/questions/6107121/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
2147483647as a whole.
On the other hand,
BitConverter.GetBytesreturns an array of bytes representing
2147483647in memory. On your machine, this number is stored in little-endian (highest byte last). And
BitConverter.ToStringoperates 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-FFfor
int.MaxValue, in big-endian, and
FF-FF-FF-7Ffor
BitConverter, in little-endian. Same number.
字节序的高低位
高位和低位,参看计算器程序员,左高右低
比如0x0102 两个字节 01是高位,02是低位
小端字节序:高位在高地址,低位在低地址
大端字节序:高位在低地址,低位在高地址
192.168.1.224
小端字节序:0xc0a801e0 对应数字3232236000
大端字节序:0xe001a8c0 假如按照小端来解析数字的话是3758205120,按照大端来解析首先反转,然后再解析会是3232236000
相关文章推荐
- KbmMemTable的简单应用(增删改查示例)
- 帮助文档的制作javadoc
- linux文件与目录权限详解
- IOS8 定位
- 给一个无符号数前置减号,结果仍为无符号
- Oracle Data Provider for .NET的使用(三)-ORACLE与.NET类型对应关系
- 【Android】媒体——利用MediaPlayer实现对讲语音播放
- 函数指针和指针函数
- MFC中修改静态文本控件的颜色和大小
- Xcode升级到7以后网络请求失败的问题
- oracle 12c rac dbca建库,提示无法访问+DATA磁盘组
- Sketch插件——移动端UI开发利器Orion3
- 截屏
- Android进阶之动态加载图片(runOnUiThread/handler)
- 设置QTableWidget的左上角CornerWidget的文字(一)
- 字母落下炸开
- iOS 面试总结
- 修改LinearLayout中子控件TextView点击颜色小结
- iOS采用手动发布app到appStore
- Wcf 第一个项目