binary writer question

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

I have a binary writer and when I write a unsigned int I was expecting
it to write 4 bytes but it is writing 5 bytes and last last byte being
0B. Also when I write string it is adding byte 01 at the, here is the
one liner I am using

writer.Write(field.Substring(0, length)) //where field is string

Any idea why I am getting OB in the unsigned int field and 01 between
two string writes??

Thanks.
 
Hi,

You should define the encoding of the string you are going to write. If for
example you use unicode you will get 2 bytes (16 bits) per character.If you
use UTF7/8 you will get 8 bits per character.
 
DBC User said:
I have a binary writer and when I write a unsigned int I was expecting
it to write 4 bytes but it is writing 5 bytes and last last byte being
0B.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Also when I write string it is adding byte 01 at the, here is the
one liner I am using

writer.Write(field.Substring(0, length)) //where field is string

Any idea why I am getting OB in the unsigned int field and 01 between
two string writes??

Look at the documentation for BinaryWriter.Write(string) for an
explanation of this. (It won't always be 1 - it will be the length in
bytes.)

Admittedly the documentation is incorrect - it claims that it writes
the length in characters as a four byte integer; in fact it writes a
7-bit-encoded integer being the length of the encoded string in
*bytes*.
 
Thanks both. I figured out the problem. I changed the long to uint and
that took care of the integer problem.
Regarding the string, I changed it to character array and it worked
fine.

Thanks again for the help.
 

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

Back
Top