我需要一个3字节的数组
这是我尝试过的
private static void GetBytesToNibbles(byte[] currentThree, out byte[] a, out byte[] b)
{
var firstLowerNibble = currentThree[0].GetNibble(0);
var firstUpperNibble = currentThree[0].GetNibble(1);
var secondLowerNibble = currentThree[1].GetNibble(0);
var secondUpperNibble = currentThree[1].GetNibble(1);
var thirdLowerNibble = currentThree[2].GetNibble(0);
var thirdUpperNibble = currentThree[2].GetNibble(1);
a= new byte[] {firstLowerNibble, firstUpperNibble, secondLowerNibble, 0x00};
b= new byte[] {secondUpperNibble, thirdLowerNibble, thirdUpperNibble, 0x00};
}
获取Nibble扩展名:
public static byte GetNibble<T>(this T t, int nibblePos)
where T : struct, IConvertible
{
nibblePos *= 4;
var value = t.ToInt64(CultureInfo.CurrentCulture);
return (byte) ((value >> nibblePos) & 0xF);
}
我按图片所示正确执行吗?如果没有,有人可以帮助我提供正确的代码吗?
答案 0 :(得分:0)
这并不完美,但是它将为您提供一个4字节的数组,您应该可以拆分自己。
图像令人困惑,因为它显示位数而不是示例值。我认为这就是为什么您认为需要两个4字节数组的原因。
public static void Main()
{
byte byte0 = 0x11;
byte byte1 = 0x22;
byte byte2 = 0x33;
int low = BitConverter.ToInt32(new byte[]{byte0, byte1,0,0},0);
int high = BitConverter.ToInt32(new byte[] {byte1, byte2,0,0},0);
low = low & 0x0fff;
high = high & 0xfff0;
high = high << 12;
int all = high | low;
byte[] intBytes = BitConverter.GetBytes(all);
for (int i = 0; i < intBytes.Length; i++)
{
Console.WriteLine(String.Format("{0:X2}", intBytes[i]));
}
}
结果:
11
02
32
03