有没有一种快速、内置的方法,可以使用C#将表示24bit (小端,二进制补码)值的三个字节组成的数组转换为整型?我该怎么做呢?
谢谢!
发布于 2010-07-24 07:12:51
我很惊讶到现在还没有人推荐BitConverter。假设您将三个字节放在单独的变量中:
var data = new byte[]
{
byte0 & 0x80 == 0 ? 0 : 0xFF, byte0, byte1, byte2
};
return BitConverter.ToInt32(data, 0);或者:
var data = new byte[] { byte0, byte1, byte2, 0x00 };
return BitConverter.ToInt32(data, 0) >> 8;发布于 2010-07-24 07:02:27
int converted = ((bytes[2] << 24) | (bytes[1] << 16) | (bytes[0] << 8)) >> 8;发布于 2015-01-09 00:58:39
有一次我被Bitconverter的小端特性所吸引。这是另一种方法: PS:格式不是小端,相反,MSB优先!此外,我还尝试手动格式化输出,但失败了
输出:
=======Test 24位Int32 (最先/左)=======预期: 8388607 Bit24ToInt32测试: 8388607预期:-1 Bit24ToInt32测试:-1预期:-8388607 Bit24ToInt32测试:-8388607预期: 6147053 Bit24ToInt32测试: 6147053
预期:-6147053 Bit24ToInt32测试:-6147053
=======Test 16位至Int32 (最先/左)=======预期: 32767 Bit16ToInt32测试: 32767预期:-1 Bit16ToInt32测试:-1预期:-32767 Bit16ToInt32测试:-32767预期: 24045 Bit16ToInt32测试: 24045
预期:-24045 Bit16ToInt32测试:-24045
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BitExperiments
{
class Program
{
static void Main(string[] args)
{
//Test 16bit to Int32(MSB First/Left)
Console.WriteLine("=======Test 24 bit to Int32 (MSB First/Left)=======");
Dictionary<int,byte[]> cases = new Dictionary<int,byte[]>();
byte[] bytearr;
int expectedResult;
bytearr = new byte[] { 0x7F, 0xFF, 0xFF };
expectedResult= 8388607;//Max
cases.Add(expectedResult,bytearr);
bytearr = new byte[] { 0xFF, 0xFF, 0xFF };
expectedResult = (-1);//Mid
cases.Add(expectedResult,bytearr);
bytearr = new byte[] { 0x80, 0x00, 0x01 };
expectedResult = (-8388607);//Min
cases.Add(expectedResult,bytearr);
bytearr = new byte[] { 0x5D, 0xCB, 0xED };
expectedResult= 6147053;//Random
cases.Add(expectedResult,bytearr);
bytearr = new byte[] { 0xA2, 0x34, 0x13 };
expectedResult= (-6147053);//Random inverted
cases.Add(expectedResult,bytearr);
foreach (var value in cases)
{
Console.WriteLine("Expected: {0} \t\t Bit24ToInt32 Test: {1}", value.Key, Bit24ToInt32(value.Value));
}
Console.WriteLine("===================================================");
Console.WriteLine();
//Test 16bit to Int32(MSB First/Left)
Console.WriteLine("=======Test 16 bit to Int32 (MSB First/Left)=======");
cases.Clear();
bytearr = new byte[] { 0x7F, 0xFF };
expectedResult = 32767;//Max
cases.Add(expectedResult, bytearr);
bytearr = new byte[] { 0xFF, 0xFF };
expectedResult = (-1);//Mid
cases.Add(expectedResult, bytearr);
bytearr = new byte[] { 0x80, 0x01 };
expectedResult = (-32767);//Min
cases.Add(expectedResult, bytearr);
bytearr = new byte[] { 0x5D, 0xED };
expectedResult = 24045;//Random
cases.Add(expectedResult, bytearr);
bytearr = new byte[] { 0xA2, 0x13 };
expectedResult = (-24045);//Random inverted
cases.Add(expectedResult, bytearr);
foreach (var value in cases)
{
Console.WriteLine("Expected: {0} \t\t Bit16ToInt32 Test: {1}", value.Key, Bit16ToInt32(value.Value));
}
Console.WriteLine("===================================================");
Console.ReadLine();
}
private static int Bit24ToInt32(byte[] byteArray)
{
int result = (
((0xFF & byteArray[0]) << 16) |
((0xFF & byteArray[1]) << 8) |
(0xFF & byteArray[2])
);
if ((result & 0x00800000) > 0)
{
result = (int)((uint)result|(uint)0xFF000000);
}
else
{
result = (int)((uint)result & (uint)0x00FFFFFF);
}
return result;
}
private static int Bit16ToInt32(byte[] byteArray) {
int result = (
((0xFF & byteArray[0]) << 8) |
(0xFF & byteArray[1])
);
if ((result & 0x00008000) > 0) {
result = (int) ((uint) result | (uint) 0xFFFF0000);
} else {
result = (int) ((uint) result & (uint) 0x0000FFFF);
}
return result;
}
}
}https://stackoverflow.com/questions/3322788
复制相似问题