問題描述
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 位格式編碼整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!