title: How To Write A String Of Binary To File C#
body: <p>I Have A String Of Binary Number Like temp = "0101110011" And I Want To Save That As File this Temp Have 10 char And How Can I Save This string To file With 10 Bit Length ?</p>
  
  <pre><code>void Save_Data(string temp)
  {
      bool[] BoolArray = new bool[temp.Length];
      BitArray Barray = new BitArray(BoolArray.Length);
      char[] ch = temp.ToCharArray();
  
      for (int i = 0; i &lt; temp.Length; i++)
      {
          if (ch[i] == '0')
          {
              Barray[i] = false;
          }
          else
          {
              Barray[i] = true;
          }
      }
  
      Stream stream = new FileStream("D:\\test.dat", FileMode.Create);
      StreamWriter sw = new StreamWriter(stream);
  
      foreach (bool bit in Barray)
      {
          sw.Write(bit ? 1 : 0);
      }
  
      sw.Flush();
      sw.Close();
  }
  </code></pre>
  
  <p>With This Code My File Length Is 80 Bit's</p>