在 .NET 中反序列化以 null 结尾的字符串的最佳方法是什么?

发布于 2024-07-19 04:01:11 字数 161 浏览 13 评论 0原文

我正在从网络连接中读取一条消息,该消息被序列化为一系列以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-07-26 04:01:11

我认为逐字节读取是一个合理的选择,但我会使用 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.

維他命╮ 2024-07-26 04:01:11

将整个内容读入字符串中,然后使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文