-2

How do I convert a boolean list to an integer equal to the Booleans interpreted as a byte?

i.e.

public List<Boolean> MyList = new List<Boolean>();
MyList.Add(true);
MyList.Add(true);
MyList.Add(false);
MyList.Add(false);
MyList.Add(false);
MyList.Add(false);
MyList.Add(false);
MyList.Add(false);

This would return a 3.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Ken
  • 27
  • 2
  • 11
  • Why would that return 4? – Sphinxxx Dec 01 '16 at 00:45
  • How this would returns 4? Are you trying to make some binary calculation? Even then how this return 4? – t.m. Dec 01 '16 at 00:47
  • For everybody asking about how it's 4. I think he wants false to be -1 and true to be +1. It would be -2 + 6 = 4. The question definitely needs some clarification. – tphx Dec 01 '16 at 00:49
  • Sorry a 3...not a 4. Binary to int. I apologize for the confusion! – Ken Dec 01 '16 at 01:12
  • @BradleyDotNET Good call. Turns out it was supposed to be binary afterall anyway. – tphx Dec 01 '16 at 01:16
  • I mean I can do a for loop and just do the math...I was just wondering if there was an elegant solution. Again sorry for the confusion! – Ken Dec 01 '16 at 01:17
  • Look at the [`BitArray` class](https://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx) – D Stanley Dec 01 '16 at 01:18
  • @Ken: For loop and do the math is the way. Note that in the future you should show how calling `Add` translates into binary so that people understand. – Guvante Dec 01 '16 at 01:18

3 Answers3

5

You can't, at least not directly.

You can however use the BitArray class (MSDN) to transform your bool collection to bits, and then get a number from that:

BitArray bitField = new BitArray(MyList.ToArray()); //BitArray takes a bool[]
byte[] bytes = new byte[1];
bitField.CopyTo(bytes, 0);
return bytes[0];

BitArray to value conversion from: https://stackoverflow.com/a/560131/1783619

Note that this technique works for numbers larger than 8-bit as well, but you would need to use BitConverter (MSDN) to get the values back from the byte array (instead of just returning the first one)

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

check this in console app. First of all, after entering the desired values into the list, you must reverse your list, then you create a string value, and then using a loop, you convert the values into 0 and 1, and finally, you convert from binary to hex.

using System.Data.SqlTypes;

List<bool> boolList = new();

boolList.Add(true);
boolList.Add(true);
boolList.Add(false);
boolList.Add(false);
boolList.Add(false);
boolList.Add(false);
boolList.Add(false);
boolList.Add(false);

boolList.Reverse();

string s = string.Empty;

boolList.ForEach(item =>
{
    s += item ? "1" : "0";
});


Console.WriteLine(Convert.ToInt32(s, 2).ToString("X"));
Console.ReadLine();
0

If you want a one line solution, you could use the aggregate function to calculate the equivalent int value.

var boolList = new List<bool> { true, false, false, false, false, true, false, false };

int integerVal = boolList.Aggregate(0, (sum,val) => (sum * 2) + (val ? 1 : 0));

Here, integerVal is 132, which is equivalent to the binary value 10000100.

Noah
  • 1
  • 2