Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How BinaryWriter.Write() write string

Tags:

.net

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?

like image 701
CMinus Avatar asked Nov 30 '22 11:11

CMinus


2 Answers

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.

like image 122
user1332054 Avatar answered Dec 17 '22 17:12

user1332054


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.

like image 25
Rex M Avatar answered Dec 17 '22 18:12

Rex M