我有一个 3 字节数组,我需要
- 将每个字节转换为 Nibbles
- 添加 Byte_0.Nibble_0 + Byte_0.Nibble_1 + Byte_1.Nibble_2 作为 WORD
- 添加 Byte_1.Nibble_0 + Byte_2.Nibble_1 + Byte_2.Nibble_2 作为 WORD
- 将每个 WORD 转换为字节数组
这是我尝试过的
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);
}
如图所示,我做得对吗?如果没有,任何人都可以帮助我使用正确的代码吗?