【发布时间】:2011-12-29 23:47:04
【问题描述】:
如何将整数读入BitArray(6)(假设可以包含)以及如何将BitArray(6) 转换为无符号/有符号整数。
【问题讨论】:
如何将整数读入BitArray(6)(假设可以包含)以及如何将BitArray(6) 转换为无符号/有符号整数。
【问题讨论】:
BitArray FromInt32(Int32 a)
{
byte[] bytes = BitConverter.GetBytes(a);
return new BitArray(bytes);
}
对于前面提到的反向操作see this question。
【讨论】:
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
【讨论】: