I searched Usenet and found some posts with people having similar
problems, I'm wondering if any MVPs or anyone else knows if this is a
confirmed bug and if MS plans to do anything about it. First let me
quite the MSDN documentation:
public virtual void Write ( string value )
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 writes a length-prefixed string to this stream
using the BinaryWriter instance's current Encoding. -----This method
first writes the length of the string as a four-byte unsigned
integer-----, and then writes that many characters to the stream.
the section enclosed in ----- ----- indicates where the "bug" arises.
There's either a bug in the documentation or the function, one or the
other.
The following sample program should demonstrate:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WriterTest
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream MStream = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MStream);
string TestString = "This is a test string";
System.Console.WriteLine("The writer is at position {0}.",
Writer.BaseStream.Position);
Writer.Write(TestString);
System.Console.WriteLine("Position after
BinaryWriter.Write(String) - {0}, String length - {1}, # of bytes
for size - {2}.", Writer.BaseStream.Position, TestString.Length,
Writer.BaseStream.Position - (long)TestString.Length);
System.Console.ReadLine();
}
}
}
}
Here's the output.
The writer is at position 0.
Position after BinaryWriter.Write(String) - 22, String length - 21,
# of bytes for size - 1.
Patrice - 12 Jul 2007 19:23 GMT
I would say the doc. From my tests it looks like the prefix is using the
hihg bit to indicate if the length keeps using the following bytes or
something similar...
--
Patrice
"Zachary Turner" <divisortheory@gmail.com> a écrit dans le message de news:
1184261105.979686.4880@n2g2000hse.googlegroups.com...
>I searched Usenet and found some posts with people having similar
> problems, I'm wondering if any MVPs or anyone else knows if this is a
[quoted text clipped - 52 lines]
> Position after BinaryWriter.Write(String) - 22, String length - 21,
> # of bytes for size - 1.
Jon Skeet [C# MVP] - 12 Jul 2007 19:31 GMT
> I searched Usenet and found some posts with people having similar
> problems, I'm wondering if any MVPs or anyone else knows if this is a
[quoted text clipped - 13 lines]
> There's either a bug in the documentation or the function, one or the
> other.
It's a bug in the docs. It's writes a "7 bit encoded" integer as per
the Write7BitEncodedInt method.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too