By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

StreamWriter.Write(bool value) --- How many bits/bytes should end up in the file?

P: n/a
Hi,

I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
..Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?

THANKS!!!!

John
Nov 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
John <jo********@hotmail.com> wrote:
I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?


StreamWriters are for *text* data, not binary data, which is what it
sounds like you want.

StreamWriter.Write(bool) writes out the *text* representation of a
boolean, as documented.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Takes 4 bytes since that is the memory layout of a bool. Based on the way
the streams work, the best they could do is 1 byte per bool if you used
Write(bool) Read(bool). However, you could do better by having a set of
methods that took an array of bools and converted that into a series of bytes
depending on how many bools you were emitting.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"John" <jo********@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Hi,

I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?

THANKS!!!!

John

Nov 16 '05 #3

P: n/a
Justin Rogers <Ju****@games4dotnet.com> wrote:
Takes 4 bytes since that is the memory layout of a bool.


Nope, it takes 4 bytes since it's writing out "True" - it would take 5
bytes if you passed in false (assuming an encoding which uses one byte
per character, of course).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Hi John.

You better use FileStream.WriteByte to do this.

Cheers,

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

"John" <jo********@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Hi,

I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?

THANKS!!!!

John

Nov 16 '05 #5

P: n/a
There is no API in .net (or C++ or Java or almost anywhere) that lets you
write a single bit. The smallest unit you can manipulate in a file is a
byte. (Ever seen a file that's got a size of 9 bits?)
If you want to save space, you may use helpers like BitVector32 or BitArray
for the bit-byte conversions.

Niki

"John" <jo********@hotmail.com> wrote in
news:d8**************************@posting.google.c om...
Hi,

I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?

THANKS!!!!

John

Nov 16 '05 #6

P: n/a
You'll not squeeze a ninth in there sunshine no matter how hard you pack it.
No-one's managed it yet, so I doubt you will.
Nov 16 '05 #7

P: n/a
Doh! You're right. I meant to say BinaryWriter.

I ended up writing a method that packs the bits into bytes and then
writes the bytes out.

I'll investigate BitArray and see how it might be helpful.

Thank you to all for your help!

John

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
John <jo********@hotmail.com> wrote:
I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char's using one bit per byte, but that
would be space-inefficient.

I'm using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.

What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that's causing
this?


StreamWriters are for *text* data, not binary data, which is what it
sounds like you want.

StreamWriter.Write(bool) writes out the *text* representation of a
boolean, as documented.

Nov 16 '05 #8

P: n/a
John <jo********@hotmail.com> wrote:
Doh! You're right. I meant to say BinaryWriter.
Well BinaryWriter only writes out a single byte when you call
BinaryWriter.Write(bool). If you believe you have an example where you
believe it writes four bytes, please post it.
I ended up writing a method that packs the bits into bytes and then
writes the bytes out.


Right.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
John <jo********@hotmail.com> wrote:
Doh! You're right. I meant to say BinaryWriter.


Well BinaryWriter only writes out a single byte when you call
BinaryWriter.Write(bool). If you believe you have an example where you
believe it writes four bytes, please post it.


Again, you're right. I started with StreamWriter. Then I realized I
should be using BinaryWriter.
I ended up writing a method that packs the bits into bytes and then
writes the bytes out.


Right.


Thanks for your help! Thanks for your patience!

John
Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.