Q:

How to read fixed32 integer using protobuf-net? 

This seems like a basic question but i have doubts maybe because i am using 2 different libraries to write and read the data.  My Write program is written in c++ which uses google's protocol buffer library.  My reader is implemented in .NET c#.  
In write program, i write a file header as follows. 
coded_output->WriteLittleEndian32(BIN_START_MAGIC_NUMBER);
coded_output->WriteLittleEndian32(major_version);
coded_output->WriteLittleEndian32(minor_version);
coded_output->WriteVarint32(strlen(_region));
coded_output->WriteRaw(_region, strlen(_region));
coded_output->WriteLittleEndian32(_offset);

What is the corresponding function in C# to read the above fields?  I understand how to read the protocol buffer messages but not sure about how to read above data.  
Regards,
Alok

A:

That file header is not a valid protobuf sequence, and consequently the standard reading mechanisms in protobuf-net won't be geared to reading it - indeed, a ProtoReader would not let itself get into a state to read that, since it is invalid.  However!  To read a little-endian 32-bit number, you can probably use:
int x, int y; // ignore
int value = ProtoReader. ReadLengthPrefix(stream, false, PrefixStyle. Fixed32,
                      out x, out y);

Assuming that the coded stream is using standard protobuf conventions, the string is written as a varint length-prefix and then UTF-8, so you could use:
int x, int y; // ignore
int length = ProtoReader. ReadLengthPrefix(stream, false, PrefixStyle. Base128,
                      out x, out y);
byte[] bytes = new byte[length];
int read, offset = 0;
while(length > 0 && (read = stream. Read(bytes, offset, length)) > 0) {
    offset += read;
    length = read;
}
if(length > 0) throw new EndOfStreamException();
string s = Encoding. UTF8.GetString(bytes);

I can probably package those more conveniently for you if you really want; they aren't exposed currently because that isn't protobuf ;p

