Convert Byte Array to Bit Array?

前端 未结 6 1985
抹茶落季
抹茶落季 2020-12-30 20:43

How would I go about converting a bytearray to a bit array?

6条回答
  •  -上瘾入骨i
    2020-12-30 21:39

    public static byte[] ToByteArray(bool[] byteArray)
    {
        return = byteArray
                   .Select(
                        (val1, idx1) => new { Index = idx1 / 8, Val = (byte)(val1 ? Math.Pow(2, idx1 % 8) : 0) }
                    )
                    .GroupBy(gb => gb.Index)
                    .Select(val2 => (byte)val2.Sum(s => (byte)s.Val))
                    .ToArray();
    }
    

提交回复
热议问题