Convert Byte Array to Bit Array?

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

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

6条回答
  •  Happy的楠姐
    2020-12-30 21:33

    It depends on what you mean by "bit array"... If you mean an instance of the BitArray class, Guffa's answer should work fine.

    If you actually want an array of bits, in the form of a bool[] for instance, you could do something like that :

    byte[] bytes = ...
    bool[] bits = bytes.SelectMany(GetBits).ToArray();
    
    ...
    
    IEnumerable GetBits(byte b)
    {
        for(int i = 0; i < 8; i++)
        {
            yield return (b & 0x80) != 0;
            b *= 2;
        }
    }
    

提交回复
热议问题