BinaryWriter is not binary enough

  • Thread starter Thread starter Guest
  • Start date Start date
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();
 
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!
***************************
 
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
 
Back
Top