Just in case these utility methods may assist you:
public static List<bool> BinaryStringToBitsAreOnes(string theString)
{
return theString.Select(ch => ch == '1').ToList();
}
public static List<string> StringToCharsAsBinary(string theString)
{
return theString.Select(ch => Convert.ToString(ch, 2).PadLeft(8, '0')).ToList();
}
Sample test: in some method or EventHandler:
List<string> results = StringToCharsAsBinary("abc");
List<bool> bools = BinaryStringToBitsAreOnes(results[0]);
// sample output as viewed in VStudio Output Window:
> ? results
Count = 3
[0]: "01100001"
[1]: "01100010"
[2]: "01100011"
> ? bools
Count = 8
[0]: false
[1]: true
[2]: true
[3]: false
[4]: false
[5]: false
[6]: false
[7]: true
>
Edit: I think the System.Collections.BitArray may assist you; example:
public static BitArray ByteToBits(byte theByte)
{
return new BitArray(new byte[]{theByte});
}
byte aByte = 63;
BitArray bArray = ByteToBits(aByte);
foreach (var val in bArray)
{
Console.WriteLine(val);
}
This would give an output like this:
True
True
True
True
True
True
False
False
Note that if you create a BitArray with multiple Bytes, you will get an Array containing eight boolean flags per Byte.
See the MS documentation on BitArray for information about its various constructors, and uses: [
^].
Edit: how to use a BitArray that holds the results of evaluating many Bytes.
public static List<bool> BytesToListOfBools(byte[] theBytes)
{
return new BitArray(theBytes).Cast<bool>().ToList();
}
byte[] bytes = new byte[] {0, 2, 4, 8, 16, 32, 64, 128};
List<bool> listOfByteAray = BytesToListOfBools(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listOfByteAray.Count; i += 8)
{
var >Output of the above:<pre lang="text">False, False, False, False, False, False, False, False
False, True, False, False, False, False, False, False
False, False, True, False, False, False, False, False
False, False, False, True, False, False, False, False
False, False, False, False, True, False, False, False
False, False, False, False, False, True, False, False
False, False, False, False, False, False, True, False
False, False, False, False, False, False, False, True