4

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.

8
  • A bool can hold two values: true and false. I am not sure what exactly you are trying to say by "convert bool array to char avariable". Can you please clarify that? Commented Jun 13, 2015 at 19:03
  • @Emz That part seems reasonably clear to me: the 8 bits of an 8-bit integer value are stored as separate elements of an array. There are a lot of things that aren't clear to me (such as why not simply use byte, why use 8 bits when ASCII is a 7-bit character set, which order the bits are in) that make the question hard to answer, though. Commented Jun 13, 2015 at 19:05
  • @Emz He probably means that the bool array represents bits in a byte. Commented Jun 13, 2015 at 19:05
  • I mean that if for example I have : false, true , true ,true, true, false, false,true it holds y character Commented Jun 13, 2015 at 19:06
  • @hvd, sticksandstones.kstrom.com/appen.html Here it stands for 8 bit Commented Jun 13, 2015 at 19:08

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.