What is the Best way to deserialize a null-terminated string in .NET?
-
05-07-2019 - |
Question
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.
OTHER TIPS
Read the whole thing into a string, then use String.Split to split on the nulls.
Remember that strings are not null-terminated in .NET.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow