Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

Bug in BinaryWriter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zachary  Turner - 12 Jul 2007 18:25 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.