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

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
 
J

Jon Skeet [C# MVP]

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.
 
J

Justin Rogers

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.
 
J

Jon Skeet [C# MVP]

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).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi John.

You better use FileStream.WriteByte to do this.

Cheers,
 
N

Niki Estner

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
 
B

Beeeeeves

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.
 
J

John

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
 
J

Jon Skeet [C# MVP]

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.
 
J

John

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
 

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