how to convert bool array to char variable?

2.1k Views Asked by At

I have a boolean array which holds some values that represent ASCII value:

bool[] myBoolReceived = new bool[8];

I try to convert it to a char so I can add it to a list that holds chars.

myReceivedMessage = new List<char>(); 

I tried to use Convert.ToChar method but it not seems to work.

1

There are 1 best solutions below

0
On BEST ANSWER

char contains 2 bytes. you can convert bool array to a byte and then convert it to a character using Convert class.

public byte ConvertToByte(bool[] arr)
{
   byte val = 0;
   foreach (bool b in arr)
   {
      val <<= 1;
      if (b) val |= 1;
   }
   return val;
}

reference