title: Getting error: Protocol message contained an invalid tag (zero) despite bytes being exactly the same
body: <p>I'm trying to deserialize my protobuf data from a byte[] in C#. The data is being from another program in c++ over a named pipe. I'm printing the byte (or unsigned char) arrays on both ends and the bytes and lengths of arrays are exactly the same. Despite that i'm getting the invalit tag(zero) error when trying to deserialize the data. The relevant code parts are:</p>
  <p>Sending data from C++:</p>
  <pre><code>bool writeOutputMessage(HANDLE hPipe, const CubesExample::OutputMessage&amp; outputMessage)
  {
      int messageBytes = outputMessage.ByteSizeLong();
      char buffer[messageBytes + 4];
      outputMessage.SerializeToArray(&amp;buffer + 4, messageBytes);
      memcpy(buffer, &amp;messageBytes, 4);
      messageBytes += 4;
  
      DWORD bytesWritten = 0;
      std::cout &lt;&lt; std::string(&quot;Writing &quot;) + std::to_string(messageBytes) + &quot; bytes&quot; &lt;&lt; std::endl;
      
      std::cout &lt;&lt; &quot;Sending bytes&quot;;
      for(char c : buffer) std::cout &lt;&lt; &quot; &quot; &lt;&lt; +static_cast&lt;unsigned char&gt;(c);
      std::cout &lt;&lt; std::endl;
  
      BOOL fSuccess = WriteFile(
          hPipe,        // handle to pipe
          buffer,     // buffer to write from
          messageBytes, // number of bytes to write
          &amp;bytesWritten,   // number of bytes written
          NULL);        // not overlapped I/O
      if (!fSuccess || bytesWritten != messageBytes)
      {
          std::cout &lt;&lt; &quot;InstanceThread WriteFile failed, GLE=&quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;
          return false;
      }
  
      return true;
  }
  </code></pre>
  <p>Receiving data in C#:</p>
  <pre><code>public byte[] readOutputMessage()
  {
      int readBytes = 0;
      byte[] messageSizeBuf = new byte[4];
  
      readBytes = fromagent_pipe.Read(messageSizeBuf, 0, 4);
      if(readBytes != 4)
      {
          Debug.Log(&quot;Failed to read message size!&quot;);
          return null;
      }
      int messageSize = BitConverter.ToInt32(messageSizeBuf, 0);
  
      Debug.Log(&quot;Attempting to read message of size: &quot; + messageSize);
      byte[] buffer = new byte[messageSize];
      readBytes = 0;
      readBytes = fromagent_pipe.Read(buffer, 0, messageSize);
      if(readBytes != messageSize)
      {
          Debug.Log(&quot;Read &quot; + readBytes + &quot; bytes but expected &quot; + messageSize);
          return null;
      }
  
      return buffer;
  }
  </code></pre>
  <p>Parsing bytes in C#:</p>
  <pre><code>byte[] buffer = inputTask.Result;
  
  string bytes = &quot;&quot;;
  foreach(byte b in buffer) bytes += &quot; &quot; + (uint)b;
  Debug.Log(&quot;Got bytes: &quot; + bytes + &quot; Length: &quot; + buffer.Length);
  
  if(buffer != null)
  {
      OutputMessage outputMessage = OutputMessage.Parser.ParseFrom(buffer);
      inputMotion = new UnityEngine.Vector2(outputMessage.MovementDirection.XPos, outputMessage.MovementDirection.YPos);
  }
  </code></pre>
  <p>Additionaly here is the output from from the C++ process:</p>
  <pre><code>Writing 11 bytes
  Sending bytes 7 0 0 0 227 0 0 0 72 57 201
  </code></pre>
  <p>And the output from the C# process:</p>
  <pre><code>Attempting to read message of size: 7
  Got bytes:  227 0 0 0 72 57 201 Length: 7
  </code></pre>
  <p>The writing process writes 4 bytes which indicate the size of the message followed by the message bytes. In this case message size was 7 so writing process wrote 11 bytes total.</p>
  <p>As you can see the bytes are exactly the same but the call to <code>OutputMessage.Parser.ParseFrom(buffer)</code> still fails with invalid tag(zero).</p>
  <p>What am i missing?</p>
  <p>Thanks to anyone reading in advance.</p>
  <p>EDIT:
  The message that is sent is being created as follows:</p>
  <pre><code>CubesExample::OutputMessage outputMessage; 
  CubesExample::Vector2* moveDir = outputMessage.mutable_movementdirection(); 
  moveDir-&gt;set_xpos(1.0f); 
  moveDir-&gt;set_ypos(0.0f);
  </code></pre>