Convert from BitArray to Byte

后端 未结 9 1610
再見小時候
再見小時候 2020-11-29 08:03

I have a BitArray with the length of 8, and I need a function to convert it to a byte. How to do it?

Specifically, I need a correct function of Co

9条回答
  •  迷失自我
    2020-11-29 08:52

    This should work:

    byte ConvertToByte(BitArray bits)
    {
        if (bits.Count != 8)
        {
            throw new ArgumentException("bits");
        }
        byte[] bytes = new byte[1];
        bits.CopyTo(bytes, 0);
        return bytes[0];
    }
    

提交回复
热议问题