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 ConvertToByte
:
BitArray bit = new BitArray(new bool[]
{
false, false, false, false,
false, false, false, true
});
//How to write ConvertToByte
byte myByte = ConvertToByte(bit);
var recoveredBit = new BitArray(new[] { myByte });
Assert.AreEqual(bit, recoveredBit);
That's should be the ultimate one. Works with any length of array.
A bit late post, but this works for me:
Works with:
.
This should do the trick. However the previous answer is quite likely the better option.
In the example you posted the resulting byte will be 0x80. In other words the first value in the BitArray coresponds to the first bit in the returned byte.