在 .NET 中反序列化以 null 结尾的字符串的最佳方法是什么?
我正在从网络连接中读取一条消息,该消息被序列化为一系列以 null 结尾的字符串(和其他二进制数据),
我可以使用 BinaryReader 一次读取一个字符,直到找到 NULL。
有没有更有效又简单的方法呢? 我期望字符串的长度少于 64 个字符,但它们可能会更长。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为逐字节读取是一个合理的选择,但我会使用 BufferedStream 以最大限度地减少底层流上的 IO。
I think reading byte by byte is a reasonable option, but I would use a BufferedStream to minimize IO on the underlying stream.
将整个内容读入字符串中,然后使用 String.Split 拆分空值。
请记住,.NET 中的字符串不是以 null 结尾的。
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.