BinaryReader ReadString指定的长度? [英] BinaryReader ReadString specifying length?

查看:614
本文介绍了BinaryReader ReadString指定的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个解析器接收UDP信息,分析它,并储存起来。要做到这一点,我使用的是 BinaryReader 因为这将主要是二进制信息。其中一些将是字符串,但。 MSDN说的 > ReadString()功能:

I'm working on a parser to receive UDP information, parse it, and store it. To do so I'm using a BinaryReader since it will mostly be binary information. Some of it will be strings though. MSDN says for the ReadString() function:

读取从当前流的字符串。该字符串的前缀是
的长度,编码为七位在一个时间一个整数。

Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

和我完全理解它直到同时七位,我想简单地忽略,直到我开始测试。把它变成一个的MemoryStream 并试图与 BinaryReader 读它之前,我创造我自己的字节数组。这是我首先想到的工作:

And I completely understand it up until "seven bits at a time" which I tried to simply ignore until I started testing. I'm creating my own byte array before putting it into a MemoryStream and attempting to read it with a BinaryReader. Here's what I first thought would work:

byte[] data = new byte[] { 3, 0, 0, 0, (byte)'C', (byte)'a', (byte)'t', }
BinaryReader reader = new BinaryReader(new MemoryStream(data));
String str = reader.ReadString();



知道了 INT 为4个字节(和玩弄足够长的时间来找出 BinaryReader 是Little Endian)我通过它的3长度和相应的字母。然而 STR 最终控股 \0\0\0 。如果我删除了3个零,只是有

Knowing an int is 4 bytes (and toying around long enough to find out that BinaryReader is Little Endian) I pass it the length of 3 and the corresponding letters. However str ends up holding \0\0\0. If I remove the 3 zeros and just have

byte[] data = new byte[] { 3, (byte)'C', (byte)'a', (byte)'t', }

然后,它读取和商店正常。对我来说这有冲突的文件说,长度应该是一个整数。现在,我开始认为他们只是意味着没有小数位一个号码,而不是数据类型 INT 。这是否意味着一个 BinaryReader 不能读取的字符串大于127个字符(因为这将对应文件的7位一部分是01111111)

Then it reads and stores Cat properly. To me this conflicts with the documentation saying that the length is supposed to be an integer. Now I'm beginning to think they simply mean a number with no decimal place and not the data type int. Does this mean that a BinaryReader can never read a string larger than 127 characters (since that would be 01111111 corresponding to the 7 bits part of the documentation)?

我写了一个协议,需要完全了解我进入之前,我通过我们的文件一起为我们的客户。

I'm writing up a protocol and need to completely understand what I'm getting into before I pass our documentation along to our clients.

推荐答案

我发现的源代码 BinaryReader 。它使用一种称为函数 Read7BitEncodedInt()以及查找该文档和的 Write7BitEncodedInt()我发现这一点:

I found the source code for BinaryReader. It uses a function called Read7BitEncodedInt() and after looking up that documentation and the documentation for Write7BitEncodedInt() I found this:

值的整数参数是在
时间写出七比特,从七个最低显著位。
A字节的高位指示是否有此
相继被写入多个字节。如果值将适合在七位,只需要一个空间字节。
如果值将不适合在七位,高位设置在第一
字节和写出来。值然后由七个位和下一个
字节写入移位。 。重复该过程,直到整个整数具有
被写入

The integer of the value parameter is written out seven bits at a time, starting with the seven least-significant bits. The high bit of a byte indicates whether there are more bytes to be written after this one. If value will fit in seven bits, it takes only one byte of space. If value will not fit in seven bits, the high bit is set on the first byte and written out. value is then shifted by seven bits and the next byte is written. This process is repeated until the entire integer has been written.

另外,拉尔夫结果此链接,更好地显示这是怎么回事。

Also, Ralf found this link that better displays what's going on.

这篇关于BinaryReader ReadString指定的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆