tags:

views:

274

answers:

1

I used BinaryWriter.Write() to write string, the msdn said when writing the string, it is said like this

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 size if fixed, but in fact it is varied.
who knows the details of how this method calculate the prefix's length.

+3  A: 

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 four-byte unsigned integer, and then writes that many characters to the stream. This method writes a length-prefixed string to this stream using the BinaryWriter instance's current Encoding.

Rex M
How does it determine whether to read/write a single byte or a 4-byte word?
BlueMonkMN