C# convert message data to byte array in protobuf encoding

3,628 views
Skip to first unread message

Matrix Guo

unread,
Mar 21, 2017, 6:12:56 AM3/21/17
to Protocol Buffers
using pb = global::Google.Protobuf;
private static byte[] FormatToBinary(T obj)
 { 
           using (MemoryStream stream = new MemoryStream())
            {               
                pb::CodedOutputStream s = new pb::CodedOutputStream(stream);
                obj.WriteTo(s);
                //s.Flush();  //once I add it,the message could not be sent any more
                return stream.ToArray();
            }
}

the result return an array with 0 element.
once I add s.Flush(); the value will be changed,while the message could not be sent any more.


what I want to do is just converting message data to byte array in protobuf encoding with proto3

Jon Skeet

unread,
Mar 21, 2017, 9:15:06 AM3/21/17
to Protocol Buffers
Just use the ToByteArray extension method... although if the code you've shown is returning an empty array, I'd expect ToByteArray to do so as well...
Reply all
Reply to author
Forward
0 new messages