Encoding an integer in 7-bit format of C# BinaryReader.ReadString(以 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 位格式编码整数
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01