Search code examples
.netbinaryreader

What is the Best way to deserialize a null-terminated string in .NET?


I am reading a message from my network connection which is serialized as a series of null-terminated strings (and other binary data)

I could read a char at a time using a BinaryReader, until I find a NULL.

Is there a more efficient way that is still simple? I'm expecting strings less than 64 chars in length, but they could be longer.


Solution

  • I think reading byte by byte is a reasonable option, but I would use a BufferedStream to minimize IO on the underlying stream.