以 C# BinaryReader.ReadString 的 7 位格式编码整数

Encoding an integer in 7-bit format of C# BinaryReader.ReadString(以 C# BinaryReader.ReadString 的 7 位格式编码整数)

本文介绍了以 C# BinaryReader.ReadString 的 7 位格式编码整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#BinaryReader有一个函数,根据MSDN,读取一个编码为七位整数"的整数,然后读取这个长度的字符串整数.

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.

七位整数格式是否有明确的文档(我粗略理解是MSB或LSB标记是否还有更多字节要读取,其余位是数据,但我会很高兴更准确的东西).

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).

更好的是,是否有 C 实现以这种格式读写数字?

Even better, is there a C implementation for reading and writing numbers in this format?

推荐答案

嗯,BinaryReader.Read7BitEncodedInt 已经说过,它希望用 BinaryWriter.Write7BitEncodedInt 并且该方法文档详细说明了格式:

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:

value 参数的整数一次写出七个位,从七个最低有效位开始.一个字节的高位表示在这个字节之后是否还有要写入的字节.

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.

所以整数 1259551277,二进制 1001011000100110011101000101101 将被转换为 7 位格式,如下所示:

So the integer 1259551277, in binary 1001011000100110011101000101101 will be converted into that 7-bit format as follows:

Remaining integer                 encoded bytes
1001011000100110011101000101101
100101100010011001110100          00101101
10010110001001100                 10101101 01110100
1001011000                        10101101 11110100 01001100
100                               10101101 11110100 11001100 01011000
0                                 10101101 11110100 11001100 11011000 00000100

不过,我现在对自己的 C 技能没有那么自信,无法提供有效的实现.但根据那个描述,这并不是很难做到.

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.

这篇关于以 C# BinaryReader.ReadString 的 7 位格式编码整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以 C# BinaryReader.ReadString 的 7 位格式编码整数