Why does BinaryWriter write garbage before my stuff

A

Anil Gupte

I am using the following code to write a file. The initial part (i.e.
strHeader) is just a string, then there is some Binary Data and then again a
very short string. I find that at the start of my file, there are some
(maybe 1 or 2) higher order ASCII characters (they vary). How can I prevent
the program from putting that garbage before my data?

Dim fsWriteStream As FileStream = New FileStream(myFileName,
FileMode.Create)
Dim bwWriter As BinaryWriter = New BinaryWriter(fsWriteStream)
bwWriter.Write(strHeader)
bwWriter.Write(myBinaryData)
bwWriter.Write(strSliceInfo)
bwWriter.Close()

TIA
 
C

Cor Ligthert [MVP]

(maybe 1 or 2) higher order ASCII characters (they vary). How can I
prevent the program from putting that garbage before my data?
Use a normal streamwriter or any other textwriter.

By the way, what do you mean with higer order ASCII characters?
I only know 127 ASCII characters which are fitted in 7 bites. Do you mean by
instance Extended ASCII characters or something like that. Don't bother
about that, as long as they fit in a byte they for sure will fit in unicode.

I assume that you are not busy for a compact device with less than 12Kb
memory.

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Anil said:
I am using the following code to write a file. The initial part (i.e.
strHeader) is just a string, then there is some Binary Data and then again a
very short string. I find that at the start of my file, there are some
(maybe 1 or 2) higher order ASCII characters (they vary). How can I prevent
the program from putting that garbage before my data?

Dim fsWriteStream As FileStream = New FileStream(myFileName,
FileMode.Create)
Dim bwWriter As BinaryWriter = New BinaryWriter(fsWriteStream)
bwWriter.Write(strHeader)
bwWriter.Write(myBinaryData)
bwWriter.Write(strSliceInfo)
bwWriter.Close()

TIA

What's written before the string is the string length. The length is
written using the Write7BitEncodedInt method, so the length may vary.

Then the string is encoded (in your case using the default UTF-8
encoding) and the bytes are written to the stream.

If you want to use a BinaryReader to read the file, this is the format
that you want. If you want to create a file in a specific format, you
should write directly to the stream instead. In that case UTF-8 might
not be the encoding that you want either.
 
A

Anil Gupte

You said: "What's written before the string is the string length. The length
is written using the Write7BitEncodedInt method,"

But I don't want it to be written - how do I make it stop doing that?

You said: "If you want to create a file in a specific format, you should
write directly to the stream instead. "

I don't understand what that means. Can you please explain?

TIA
 
A

Anil Gupte

Can you explain what you mean by "Use a normal streamwriter or any other
textwriter"? I need to write a combination of strings (for a header) and
binary data (encrypted). A textwriter will not work, ya?

By higher order Ascii characters, I mean gobbledygook as in characters
beyond Ascii 127.

" I assume that you are not busy for a compact device with less than 12Kb
memory."
Huh?

TIA,
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Anil said:
You said: "What's written before the string is the string length. The length
is written using the Write7BitEncodedInt method,"

But I don't want it to be written - how do I make it stop doing that?

By not using a BinaryWriter.
You said: "If you want to create a file in a specific format, you should
write directly to the stream instead. "

I don't understand what that means. Can you please explain?

Use a FileStream object to write to the file. Use an encoding object to
encode the strings into byte arrays so that you can write them to the file.
 

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