title: Difference between serialized bytes and ASCII bytes
body: <p>I ran into a little problem this morning and I'm afraid I need a little help. Every time I want to serialize , let's say, a DataTable for storage in a database and deserialize upon retrieval, I use the following method :</p>
  
  <pre><code>public static byte[] DatatableToByteArray(DataTable d)
          {
              try
              {
                  System.IO.MemoryStream stream = new System.IO.MemoryStream();
                  System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                  formatter.Serialize(stream, d);
                  return stream.GetBuffer();
              }
              catch (Exception ex)
              {
                  Log.WriteError(ex);
                  return null;
              }
          }
  </code></pre>
  
  <p>This works like a charm and the byte[] returned is easy to store in <code>blob</code> or another type of binary database field.</p>
  
  <p>If I want to transfer <code>byte[]</code> across a TCP connection, I usually do the following :</p>
  
  <pre><code>public bool SendTCP(string server, int port, string message)
  {
      try
      {
          TcpClient _tcp = new TcpClient();
          _tcp.Connect(server, port);
  
          Stream stm = _tcp.GetStream();
          ASCIIEncoding asen = new ASCIIEncoding();
          byte[] ba = asen.GetBytes(message);
  
          stm.Write(ba, 0, ba.Length);
  
          //Took out the write back thingy
  
          return true;
      }
      catch(Exception ex)
      {
          Log.Write(ex, false);
          return false;
      }
  }
  </code></pre>
  
  <p>This also works like a charm and I get my bytes across without a hitch. Now here's my problem. Everything goes south when I mix the two. For example :</p>
  
  <pre><code>public void Mixed(string server, int port, string message)
  {
      try
      {
          //Create a datatable
          DataTable dt = new DataTable();
          dt.Columns.Add("TestColumn", typeof(String));
  
          DataRow row = dt.NewRow();
          row["TestColumn"] = "test";
          dt.Rows.Add(row);
  
          //Serialize it 
          Byte[] bb = DatatableToByteArray(dt);
  
          //Send it over the wire
          TcpClient _tcp = new TcpClient();
          _tcp.Connect(server, port);
  
          Stream stm = _tcp.GetStream();
          //ASCIIEncoding asen = new ASCIIEncoding();
          //byte[] ba = asen.GetBytes(message);
  
          stm.Write(bb, 0, bb.Length);
      }
      catch(Exception ex)
      {
          Log.Write(ex, false);
      }
  }
  </code></pre>
  
  <p>When the other end is deserializing the bytes received, it reports an error :</p>
  
  <blockquote>
    <p>System.Runtime.Serialization.SerializationException: Binary stream '0'
    does not contain a valid BinaryHeader. Possible causes are invalid
    stream or object version change between serialization and
    deserialization.    at
    System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()<br>
    at
    System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler
    handler, __BinaryParser serParser, Boolean fCheck, Boolean
    isCrossAppDomain, IMethodCallMessage methodCallMessage)    at
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream
    serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
    isCrossAppDomain, IMethodCallMessage methodCallMessage)    at
    GCP.Classes.Methods.ByteToDataTable(Byte[] b) in
    D:\Appz\App1\Classes\Mixed.cs:line x</p>
  </blockquote>
  
  <p>I took out the <code>ASCIIEncoding</code> and tried to send the serialized DataTable directly across the line but the receiving side wouldn't have it. Apparently byte[] isn't byte[]. And I'm curious why. Why is ASCII encoded <code>byte[]</code> being re-assembled perfectly and serialized <code>byte[]</code> not. </p>
  
  <p>I hope someone can shed a little light on the matter. I highly appreciate any input and thank you in advance. </p>