Example:
将字节数组转化为int型
byte[] bytes = { 0, 0, 0, 25 };
// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
将int转化为字节数组
byte[] bytes = BitConverter.GetBytes(201805978);
Console.WriteLine("byte array: " + BitConverter.ToString(bytes));
// Output: byte array: 9A-50-07-0C
BitConverter:
Type returned | Method |
bool | ToBoolean(Byte[], Int32) |
char | ToChar(Byte[], Int32) |
double | ToDouble(Byte[], Int32) |
short | ToInt16(Byte[], Int32) |
int | ToInt32(Byte[], Int32) |
long | ToInt64(Byte[], Int32) |
float | ToSingle(Byte[], Int32) |
ushort | ToUInt16(Byte[], Int32) |
uint | ToUInt32(Byte[], Int32) |
ulong | ToUInt64(Byte[], Int32) |