這個問題非常接近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