有沒有比使用BitConverter
寫更好的方法?C#:手動位轉換?
public static class ByteArrayExtensions
{
public static int IntFromUInt24(this byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException();
}
if (bytes.Length != 3)
{
throw new ArgumentOutOfRangeException
("bytes", "Must have length of three.");
}
return BitConverter.ToInt32
(new byte[] { bytes[0], bytes[1], bytes[2], 0 }, 0);
}
}
您可以使用位移運算符'<<'移動字節[0-2]的值。 – davisoa 2010-10-15 21:55:04
http://stackoverflow.com/questions/3559183/reading-an-unsigned-24-bit-integer-from-ac-stream – 2010-10-15 22:07:04