BitConvert类命名空间:system
常用方法:BitConvert.GetBytes(Int16|double|short|bool)
BitConvert.ToInt16(byte[]);
BitConvert.ToString(byte[])将byte[] 转成等效的十六进制字符串。
字段:IsLittleEndian
指示数据存储在此计算机体系结构中的字节顺序(从大端开始,还是从小端开始)
先看demo示例。
using System;
public class Example
{
public static void Main()
{
int value = -16;
// 将int类型(value)转成字节数组byte[]
Byte[] bytes = BitConverter.GetBytes(value);
//将字节数组转成等效的十六进制字符串
Console.WriteLine(BitConverter.ToString(bytes));
// 将字节数组bytes 转成Int32类型
int intValue = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("{0} = {1}: {2}",
value, intValue,
value.Equals(intValue) ? "Round-trips" : "Does not round-trip");
// window系统默认是小-endian(低位字节在前),网络传输时字节顺序时大-endian
int value2 = 12345678;
byte[] bytes2 = BitConverter.GetBytes(value2);
//小-endian (little-endian)
Console.WriteLine(BitConverter.ToString(bytes2));
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
//大-endian (big-endian)
Console.WriteLine(BitConverter.ToString(bytes));
}
}
// The example displays the following output:
// F0-FF-FF-FF
// -16 = -16: Round-trips
// 4E-61-BC-00 //小-endian(低字节先传输)
// 00-BC-61-4E//大-endian(高字节先传输)
也可以通过
调用IPAddress.HostToNetworkOrder(Int value)方法将数据转换为网络字节顺序和IPAddress.NetworkToHostOrder(Int value)方法将其转换为所需的接收方的顺序。
1222



被折叠的 条评论
为什么被折叠?



