title: HEX to bit[ ] array (also known as bool[ ])
body: <p>I'm kindda new to c# and i was looking for some ideas on 2 thing. I have looked far and wide for answers but haven't found an answer to my exact problem.</p>
  
  <ol>
  <li><p>I have a byte array (called BA) within a for loop which keeps over-writting itself and there is no way for my to be able print it as a whole array. Is there a way to export it outside the for loop (maybe with a different name) so i can use it later on in the program? i just want something like this:</p>
  
  <pre><code>byte[] BA2 = {3 187,3 203,111 32, ...etc}; //(groups of 2bytes separated by commas).
  </code></pre>
  
  <p>From the original</p>
  
  <pre><code>string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
  </code></pre>
  
  <p>(and also to represent this information in bits (boolean) so {00000011 10111011,00000011 11001011, ...etc})</p></li>
  <li><p>The second thing i must do is to shift these two bytes by 4 and apply and AND gate with FFF0 (which is the same as multiplying the first byte * 1, and the second by 0xF0). Then put this in a ushort[ ] (unsigned short array) which holds the transformed bytes in binary format and then from there convert it back to HEX.</p></li>
  </ol>
  
  <p>I understand that this might be unclear (my code is kind of messy), and pretty complex. but i was hoping some of you c# guru's could lend me hand.</p>
  
  <p>Here's my code so far, i have put in comments the bits that don't work so the code runs. but i desperatly need to fix them.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
  
          string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
          string[] hex2byte = hexValues.Split(',');
  
          for (int j = 0; j &lt; 6; j++)
          {
              Console.WriteLine("\n2 byte String is: "+ hex2byte[j]);
              string[] hex1byte = hex2byte[j].Split(' ');
  
              for (int k = 0; k &lt; 2; k++)
              {
                  Console.WriteLine("byte " + hex1byte[k]);
                  byte[] BA = StringToByteArray((hex1byte[k]));
  
                  //bool[] AA = BitConverter.ToBoolean(BA);           // I'm essentially stuck here. I need somehting which actually works.
  
                  //for (int i2 = 0; i2 &lt; 2; i2++);                   // This is my attemp to perform de shift and AND.
                  //{
                  //    ushort[] X = new ushort[1];
                  //    X[0] = (ushort)((ushort)(BA[0] &lt;&lt; 4) + (ushort)((BA[1] &amp; 0xF0) &gt;&gt; 4));        // They have to be in this order: ((1stByte &amp; 0xFF) &lt;&lt; 4) + ((2byte &amp; 0xF0) &gt;&gt; 4); first to the left then the right.
  
                  //}
  
                  Console.WriteLine("Converted " + BA[0]);
              }
          }
          //Console.WriteLine(BA[4]);               // it says: Does not exist in current context. Can it only b accesed in the for loop?
          Console.ReadKey();
  
      }   // Main method finishes.
  
  
      // Define StringToByteArray method.
      public static byte[] StringToByteArray(String hex)
      {
          int NumberChars = hex.Length;
          byte[] bytes = new byte[NumberChars / 2];
          for (int i = 0; i &lt; NumberChars; i += 2)
          {
              bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
          }
          return bytes;
      }
  }
  </code></pre>