[英]How to parse bits array to byte array in c#?
This question is very close to Bit array to Byte array in JAVA , I want to convert the following bit array to a byte array? 这个问题非常接近JAVA中的位数组到字节数组,我想将以下位数组转换为字节数组吗?
int[] bits = {1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
But different to the answer is the relevant question, I want to store the result big-endian which should be: 但是与答案不同的是相关的问题,我要存储结果big-endian ,它应该是:
0xB5 0x02
How I suppose to do this? 我应该怎么做? Thanks!
谢谢!
Try this code: 试试这个代码:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.