[.net] Reading strings written by BinaryWriter.Write(string)?

Started by
15 comments, last by HeftiSchlumpf 15 years, 9 months ago
My game's toolset is written in C#. When I "compile" a set of content to a file, I use .NET's BinaryWriter class to write strings to the file. They're length-prefixed, but not (apparently) by a normal 4-byte int. My game itself is written in C++, and it needs to read these strings in. I can't seem to dig up on Google the format .NET uses for its length prefix on strings. Does someone have a link to sample code for reading such strings or a specification on the format?
Advertisement
This link says that it uses either a word or byte.

I don't know how you are supposed to know which one was used when you try to read it, though...
When you write out a string using BinaryWriter, the string is length prefixed with a 7 bit encoded integer. This enables it to saves space for when the length is not consuming the full 32 bit integer (as most won't)

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

You know better than I do, but on the page it says:

Quote: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.
Can I just suggest you use BinarySerialization to save the data and save yourself some headaches.

theTroll
According to the current version of MSDN on BinaryWriter.Write(String) -
Quote:This method first writes the length of the string as a four-byte unsigned integer, and then writes that many characters to the stream.
So far we have arguments for a byte, word, int and even variable length length prefix. [smile]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

I know it is not constant--experimentation showed that a 50-character string gets a 1-byte prefix, and that longer strings get more bytes. I'd rather not examine the output of the function for every possible length of string, so the search for documentation continues.
Actually, I see byte, short, and int all in one paragraph:

Quote: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.


Nice.
You can use Reflector to inspect the code of BinaryWriter.Write(string). You will be fixed.

edit : according to the code for strings with less than 128 characters, it will prefix with a single byte, greater or equal to 128 it will prefix with 4 bytes.
This link says:

Quote:The string is prefixed with the length, encoded as an integer seven bits at a time.


Does anyone know for sure how this works? :(

This topic is closed to new replies.

Advertisement