From bool[] to byte[]: Convert bool[] to byte[]
But I need to convert a byte[] to a List where the first item in the list is the LSB.
I tried the code below but when converting to bytes and back to bools again I have two totally different results…:
public List<bool> Bits = new List<bool>();
public ToBools(byte[] values)
{
foreach (byte aByte in values)
{
for (int i = 0; i < 7; i++)
{
Bits.Add(aByte.GetBit(i));
}
}
}
public static bool GetBit(this byte b, int index)
{
if (b == 0)
return false;
BitArray ba = b.Byte2BitArray();
return ba[index];
}
You’re only considering 7 bits, not 8. This instruction:
Should be:
Anyway, here’s how I would implement it: