以 C# BinaryReader.ReadString 的 7 位格式编码整数
C#
的BinaryReader
有一个功能,根据MSDN,读取一个编码为“七位整数”的整数,然后读取一个具有该整数长度的字符串。
是否有关于七位整数格式的明确文档(我粗略地理解MSB或LSB标记是否有更多字节要读取,其余位是数据,但我会很高兴提供更准确的东西)。
更好的是,是否有一个 C 实现可以读取和写入这种格式的数字?
C#
's BinaryReader
has a function that according to MSDN, reads an integer encoded as "seven bit integer", and then reads a string with the length of this integer.
Is there a clear documentation for the seven bit integer format (I have a rough understanding that the MSB or the LSB marks whether there are more bytes to read, and the rest bits are the data, but I'll be glad for something more exact).
Even better, is there a C
implementation for reading and writing numbers in this format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧, BinaryReader.Read7BitEncodedInt 的文档已经说,它希望使用 BinaryWriter 写入该值。 Write7BitEncodedInt 和该方法文档详细说明了格式:
因此,整数 1259551277(二进制 1001011000100110011101000101101)将被转换为 7 位格式,如下所示:
不过,我现在对我的 C 技能不太有信心来提供有效的实现。但根据该描述,这并不难做到。
Well, the documentation for BinaryReader.Read7BitEncodedInt already says, that it expects the value to be written with BinaryWriter.Write7BitEncodedInt and that method documentation details the format:
So the integer 1259551277, in binary 1001011000100110011101000101101 will be converted into that 7-bit format as follows:
I'm not that confident in my C skills right now to provide a working implementation, though. But it's not very hard to do, based on that description.
基本上,7 位编码
Int32
背后的想法是减少小值所需的字节数。它的工作原理如下:Int32.MaxValue
也不需要超过 5 个字节)。如果第 5 个字节的最高位仍然设置,则您读取的内容不是 7 位编码的 Int32。请注意,由于它是逐字节写入的,因此这些值的字节序根本不重要。给定值范围需要以下字节数:
Int32.MinValue
) 到 -1如您所见,实现有点愚蠢,并且负值总是需要 5 个字节,因为符号位是第 32 位原始值,始终以第 5 个字节结束。
因此,我不建议将其用于负值或大于 ~250,000,000 的值。我只看到它在内部用于 .NET 字符串的字符串长度前缀(您可以使用
BinaryReader.ReadString
和BinaryReader.WriteString
读取/写入这些字符串),描述了字符串后面包含的字符数,仅具有正值。虽然您可以查找原始.NET源,我在 二进制数据库。
Basically, the idea behind a 7-bit encoded
Int32
is to reduce the number of bytes required for small values. It works like this:Int32.MaxValue
would not require more than 5 bytes when only 1 bit is stolen from each byte). If the highest bit of the 5th byte is still set, you've read something that isn't a 7-bit encoded Int32.Note that since it is written byte-by-byte, endianness doesn't matter at all for these values. The following number of bytes are required for a given range of values:
Int32.MaxValue
) and -2,147,483,648 (Int32.MinValue
) to -1As you can see, the implementation is kinda dumb and always requires 5 bytes for negative values as the sign bit is the 32nd bit of the original value, always ending up in the 5th byte.
Thus, I do not recommend it for negative values or values bigger than ~250,000,000. I've only seen it used internally for the string length prefix of .NET strings (those you can read/write with
BinaryReader.ReadString
andBinaryReader.WriteString
), describing the number of characters following of which the string consists, only having positive values.While you can look up the original .NET source, I use different implementations in my BinaryData library.
我还必须探索这种 7 位格式。在我的一个项目中,我使用 C# 的 BinaryWriter 将一些数据打包到文件中,然后使用 BinaryReader 再次将其解压,效果很好。
后来我还需要为这个项目的 Java 打包文件实现一个阅读器。 Java有一个名为DataInputStream的类(在java.io包中),它有一些类似的方法。不幸的是,DataInputStream 的数据解释与 C# 的非常不同。
为了解决我的问题,我自己编写了一个扩展 java.io.DataInputStream 的类,将 C# 的 BinaryReader 移植到了 Java。这是我写的方法,它的作用与 C# 的 BinaryReader.readString() 完全相同:
I had to explore this 7-bit format also. In one of my projects I pack some data into files using C#'s BinaryWriter and then unpack it again with BinaryReader, which works nicely.
Later I needed to implement a reader for this project's packed files for Java, too. Java has a class named DataInputStream (in java.io package), which has some similar methods. Unfortunately DataInputStream's data interpretation is very different than C#'s.
To solve my problem I ported C#'s BinaryReader to Java myself by writing a class that extends java.io.DataInputStream. Here is the method I wrote, which does exactly the same as C#'s BinaryReader.readString():
格式描述如下:
http://msdn.microsoft.com/en-我们/library/system.io.binarywriter.write7bitencodedint.aspx
The format is described here:
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.write7bitencodedint.aspx
Write7BitEncodedInt 方法包含描述:每个字节的最低 7 位对数字的下 7 位进行编码。当后面有另一个字节时,最高位被设置。
Write7BitEncodedInt method contains the description: The lowest 7 bits of each byte encode the next 7 bits of the number. The highest bit is set when there's another byte following.