Jiew Meng

Web Developer, Computer Science Student

22 Nov 2010

Differences between BinaryWriter.Write(string) vs StreamWriter.Write(string)

An important point from the MSDN Documentation

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

So the length of string is prefixed then the text. So to avoid the prefix, just encode your string into bytes

BinaryWriter memBW = new BinaryWriter(memStream, encoder);
memBW.Write(encoder.GetBytes("Hello World"));

Search

Loading