title: Convert binary data to a pdf file
body: <p>I am trying to convert a binary data to its original format ".PDF," but either of the solutions I have braek my hed. The first is a little one, it creates a PDF file but it appears empty. The second one also creates a PDF file, but I can't open it. Where is the error?</p>
  
  <p>First code:</p>
  
  <pre><code>Conn.Open();
  SqlCommand cmd = Conn.CreateCommand();
  cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
  byte[] binaryData = (byte[])cmd.ExecuteScalar();
  
  string s = Encoding.UTF8.GetString(binaryData);
  
  File.WriteAllText("algo.pdf", s);
  </code></pre>
  
  <p>Second code:</p>
  
  <pre><code>Conn.Open();
  SqlCommand cmd = Conn.CreateCommand();
  cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
  byte[] binaryData = (byte[])cmd.ExecuteScalar();
  
  // Convert the binary input into Base64 UUEncoded output.
  string base64String;
  try
  {
      base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
  }
  catch (System.ArgumentNullException)
  {
      MessageBox.Show("Binary data array is null.");
      return;
  }
  
  cmd.CommandText = "Select Titulo From Artigo WHERE (IDArtigo ='" + id + "')";
  string titulo = (string)cmd.ExecuteScalar();
  
  // Write the UUEncoded version to the output file.
  System.IO.StreamWriter outFile;
  try
  {
      outFile = new StreamWriter(titulo + ".pdf", false, System.Text.Encoding.ASCII);
      outFile.Write(base64String);
      outFile.Close();
  }
  catch (System.Exception exp)
  {
      System.Console.WriteLine("{0}", exp.Message);
  }
  </code></pre>