I am new to network programming so I have a question: is it safe to send an integer (let’s assume 16 bit integer because 32 has issue with “endianess”) in my case between C++ and C# and how do that?
In my program I cast int to char (I know is is lower than 255) and send it. However, I would like to send 32 int. I tried conduct that with protocol buffers. I used WriteVarint32 in C++ and ProtoReader.ReadUInt32 in C# with no success (exception in C#). I thought it will work after I read this.
Would you be so kind and suggest me the right way to send an integer between C++ and C# applications? If you explained me how to do that with protocol buffers (protobuf-net in case of C#), I would be very pleased.
If you are writing with protobuf-net, the WithLengthPrefix methods will do this for you. I’d be more than happy to help with this (I’m the author). Btw, the reason it is throwing an exception is that a varint value by itself is not normally valid in a protobuf stream. The system will override this in the case of WithLengthPrefix if you pass 0 as the field number (or pass a positive field-number for a more pure protobuf stream; this is optional, though).
As it happens there is a method on ProtoReader to read an isolated varint (without worrying about protobuf stream semantics) it would be trivial for me to add such to ProtoWriter if that would be useful.
Btw – in terms of PrefixStyle, Base128 is the same as varint. Some folks prefer 32-bit prefixes, to marry up with other systems – hence a few others are provided.