title: Converting string from memorystream to binary[] contains leading crap
body: <p>--Edit with more bgnd information--</p>
  
  <p>A (black box) COM object returns me a string.
  A 2nd COM object expects this same string as byte[] as input and returns a byte[] with the processed data.
  This will be fed to a browser as downloadable, non-human-readable file that will be loaded in a client side stand-alone application.</p>
  
  <p>so I get the string inputString from the 1st COM and convert it into a byte[] as follows</p>
  
  <pre><code>BinaryFormatter bf = new BinaryFormatter();
  MemoryStream ms = new MemoryStream();
  bf.Serialize(ms, inputString);
  obj = ms.ToArray();
  </code></pre>
  
  <p>I feed it to the 2nd COM and read it back out.
  The result gets written to the browser.</p>
  
  <pre><code>Response.ContentType = "application/octet-stream";
  Response.AddHeader("content-disposition", "attachment; filename="test.dat");
  Response.BinaryWrite(obj);
  </code></pre>
  
  <p>The error occurs in the 2nd COm because the formatting is incorrect.
  I went to check the original string and that was perfectly fine. I then pumped the result from the 1st com directly to the browser and watched what came out. It appeared that somewhere along the road extra unreadable characters are added. What are these characters, what are they used for and how can I prevent them from making my 2nd COM grind to a halt?</p>
  
  <p>The unreadable characters are of this kind:</p>
  
  <p>NUL/SOH/NUL/NUL/NUL/FF/FF/FF/FF/SOH/NUL/NUL/NUL etc</p>
  
  <p>Any ideas?</p>
  
  <p><strong>--Answer--</strong><br>
  Use </p>
  
  <pre><code>System.Text.Encoding.UTF8.GetBytes(theString)
  </code></pre>
  
  <p>rather then</p>
  
  <pre><code>BinaryFormatter.Serialize()
  </code></pre>