gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:16:06 26 4
gpt4 key购买 nike

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

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

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

最佳答案

好吧,BinaryReader.Read7BitEncodedInt 的文档已经说过,它希望用 BinaryWriter.Write7BitEncodedInt 写入值该方法文档详细说明了格式:

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 位格式:

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 技能并没有那么自信,无法提供有效的实现。但根据该描述,这并不难做到。

关于c# - 以 C# BinaryReader.ReadString 的 7 位格式编码整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1550560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com