PC Review


Reply
Thread Tools Rate Thread

binary writer question

 
 
DBC User
Guest
Posts: n/a
 
      25th Jul 2006
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.

 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      25th Jul 2006
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.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"DBC User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jul 2006
DBC User <(E-Mail Removed)> wrote:
> 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*.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
DBC User
Guest
Posts: n/a
 
      25th Jul 2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Writer generating empty files one one server but not on oth flutophilus Microsoft Dot NET 5 12th Mar 2009 08:59 AM
Binary writer spurious IOException issue Mike Microsoft Dot NET Framework 1 21st Jun 2006 09:55 PM
binary writer only writes one file =?Utf-8?B?aXdkdTE1?= Microsoft VB .NET 0 1st Apr 2006 05:26 AM
weirdly acting binary writer =?Utf-8?B?aXdkdTE1?= Microsoft VB .NET 0 7th Mar 2006 10:04 PM
binary writer doesnt seem to do anything =?Utf-8?B?U3R1?= Microsoft Dot NET Compact Framework 4 9th Sep 2005 07:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:46 AM.