How to convert bool array to char variable?

I have a boolean array that contains some values ​​that represent an ASCII value:

bool[] myBoolReceived = new bool[8];

I am trying to convert it to char, so I can add it to a list containing characters.

myReceivedMessage = new List<char>(); 

I tried to use the method Convert.ToChar, but it does not work.

+4
source share
1 answer

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

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

link

+4
source

All Articles