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

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
 
John said:
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.
 
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 said:
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).
 
Hi John.

You better use FileStream.WriteByte to do this.

Cheers,
 
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
 
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.
 
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
 
John said:
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 said:
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.

Thanks for your help! Thanks for your patience!

John
 
Back
Top