Jump to content
  • Advertisement
Sign in to follow this  
BeanDog

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

This topic is 3625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Can I just suggest you use BinarySerialization to save the data and save yourself some headaches.

theTroll

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!