473,657 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
9 4643
John <jo********@hot mail.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.Wr ite(bool) writes out the *text* representation of a
boolean, as documented.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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********@hot mail.com> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
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
Justin Rogers <Ju****@games4d otnet.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi John.

You better use FileStream.Writ eByte to do this.

Cheers,

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

"John" <jo********@hot mail.com> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
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
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********@hot mail.com> wrote in
news:d8******** *************** ***@posting.goo gle.com...
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
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
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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
John <jo********@hot mail.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.Wr ite(bool) writes out the *text* representation of a
boolean, as documented.

Nov 16 '05 #8
John <jo********@hot mail.com> wrote:
Doh! You're right. I meant to say BinaryWriter.
Well BinaryWriter only writes out a single byte when you call
BinaryWriter.Wr ite(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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
John <jo********@hot mail.com> wrote:
Doh! You're right. I meant to say BinaryWriter.


Well BinaryWriter only writes out a single byte when you call
BinaryWriter.Wr ite(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
8736
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a code '1101', what I should write? 13? no, I don't think so
3
4394
by: Lauren Quantrell | last post by:
I'd like to use my Access 2K application to write some text in the file properties of a Windows file. The properties I'm talking about are the properties you see whn you right click on a Windows file and then click on Properites, then the Summary tab. I want to be able to modify the Subject line and the Comments line in particular. I'm hoping someone can tell me how to do this. Thanks, lq
16
3485
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
0
1325
by: shefali | last post by:
Hi, I need to use an HTTPHandler to create and write files. I wanted to create the file in the directory that has this handler, and other handlers. For me, this directory is: c:\Inetpub\wwwroot\HttpHandlers. I get an error: "Access to the path "C:\Inetpub\wwwroot\HTTPHandlers\bidhero.ccn" is denied." I can have the code write files to other locations just fine. I'm using ASP.net v.1.1 on Windows XP, and Windows 2003.
2
1043
by: Leon | last post by:
Can you write over a value in session or do you have to delete the value first then re-create it? **Example: I have the following value in session state... Statement 1 -- Session.Add("Email", "someone@ someplace.com ") And I want to change it to the following value... Statement 2 -- Session.Add("Email", "someone@ msn.com ")
0
2046
by: Kevin | last post by:
I'm writing a service in VB.NET. Reference the following code: Dim oStreamWriter As StreamWriter .... .... .... oStreamWriter = File.CreateText(TempLogFile) If Err.Number <> 0 Then EventLog.WriteEntry("LogService", "Error Creating Log File: " & _ TempLogFile & vbCrLf & _
3
1649
by: HareshKarkar | last post by:
Hi , I'm calling some Javascript function to write value in the layer. I'm trying to achieve it by using eval() function. Please look at the code below: function showMore(tempLayerNum, tempLayerName) { var temp = "step"+tempLayerNum+"More".toString(); eval(temp).write("Hi"); }
0
1119
by: Swan | last post by:
Can anyone plz tell me,Can I write file on server using binary access,or it is only to write on local?(Actually I am creating OCX for Http File Upload Control in VB using API's.So for that I need to raed file from local and write it to server.) Thanking you!
12
6045
by: Monica Leko | last post by:
Hi I have a specific format and I need binary representation. Does Python have some built-in function which will, for instance, represent number 15 in exactly 10 bits?
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.