I used BinaryWriter.Write() to write strings, in msdn, the description is as below:
Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.
I thought the length-prefix's is fix-sized; but in fact it's variable-sized. Any detail on how this method calculate the prefix's length?
The prefix is encoded using unsigned LEB128 format: http://en.wikipedia.org/wiki/LEB128
Basically, if the length is 127 or less, a single byte is written. If it is greater, then the high bit is set, and the next 7 bits of the length are written. If there still aren't enough bits (length is 16k or greater), then the high bit is set again, and another 7 bits are written.
So, there will be as many bytes as needed to store the length, and each byte will have 7 bits of the length, and the high bit will tell if another byte of the length is present.
For .NET 4.0 and above, MSDN also says:
A length-prefixed string represents the string length by prefixing to the string a single byte or word that contains the length of that string. This method first writes the length of the string as a UTF-7 encoded unsigned integer, and then writes that many characters to the stream by using the BinaryWriter instance's current encoding.
For earlier versions, it said:
A length-prefixed string represents the string length by prefixing to the string a single byte or word that contains the length of that string. This method first writes the length of the string as a four-byte unsigned integer, and then writes that many characters to the stream by using the BinaryWriter instance's current Encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With