BinaryWriter is not binary enough

G

Guest

The Binarywriter appends an extra carriage return at the end. Is there a way
to supress that?

string file = @"c:\temp\binary.txt";
FileStream fs= new FileStream(file,
FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("BINARY PLEASE");
bw.Flush();
bw.Close();
 
G

Guest

I have never used a binary writer in this fashion, but I do not remember
having a problem when the message I was sending was converted to binary
before sending to the stream. You might want to test conversion first rather
than relying on the stream/writer to convert for you.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
J

Jon Skeet [C# MVP]

Arne said:
The Binarywriter appends an extra carriage return at the end. Is there a way
to supress that?

string file = @"c:\temp\binary.txt";
FileStream fs= new FileStream(file,
FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("BINARY PLEASE");
bw.Flush();
bw.Close();

It's not putting a carriage return at the end, it's putting a byte 13
at the *start* of the stream, just as described by
BinaryWriter.Write(string) - it's the length prefix for the string
data, basically.

Here's a dump of the file written by the above code:

00000000 0D 42 49 4E 41 52 59 20 50 4C 45 41 53 45 .BINARY PLEASE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top