这个问题非常接近Bit array to Byte array in JAVA,我想将下列位数组转换为字节数组?如何解析位数组到C#中的字节数组?
int[] bits = {1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
但不同的答案是相关的问题,我想存储的结果大端这应该是:
0xB5 0x02
如何我想做到这一点?谢谢!
这个问题非常接近Bit array to Byte array in JAVA,我想将下列位数组转换为字节数组?如何解析位数组到C#中的字节数组?
int[] bits = {1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
但不同的答案是相关的问题,我想存储的结果大端这应该是:
0xB5 0x02
如何我想做到这一点?谢谢!
试试这个代码:
byte[] result = bits.Select((x, i) => new {byteId = i/8, bitId = i % 8, bit = x})
.GroupBy(x => x.byteId)
.Select(x => (byte) x.Reverse().Aggregate(0, (t, n) => t | n.bit << n.bitId))
.ToArray();
你是问如何int'转换''到ushort'?你可以分享的任何代码尝试? – KernelMode
位数组不是C#类型也许你的意思是字节?或System.Collections.BitArray? – Jimbot
对于我的表达感到抱歉,我重新修正了我的问题。 – BarryLib