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

Is it possible to write bit by bit while in Binary Mode?

P: n/a
Hi Gang,
I would like to know how to write a single bit to a file (if that's
possible). I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.
Thanks in Advance for an example or explanation,
Christian Blackburn
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Cor
Hi Cristan
Yes you can write a little bit to a file, but when you mean 1/8 byte than
it's impossible.
I would like to know how to write a single bit to a file (if that's
possible). I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.

I hope this helps a 1/8 byte.
Cor
Nov 20 '05 #2

P: n/a
Just to add to Armin's suggestion, you need to also write the length of data
to the file somewhere(first byte??), read this and then read the relevent
bits of data from the remainder of the file.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Op*************@tk2msftngp13.phx.gbl...
"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb
Hi Gang,
I would like to know how to write a single bit to a file (if
that's possible). I would like to repetitiously write a 1 or 0 as the
data I'm trying to save would dictate.
Thanks in Advance for an example or explanation,
Christian Blackburn
As Cor stated, it is not possible to write a single bit. You could use a
Boolean-Array as a buffer containing 8 values. Wrap the buffer in a class,
add a Write method that writes the passed bit (represented by a Boolean)

at the next position in the buffer and increases the buffer pointer. When 8
bits have been written, create a byte from the 8 bits and write the byte to the file.

I don't know if it makes sense. Be careful because 2 bits will be losted
when writing 10 bits. ;-))
--
Armin

Nov 20 '05 #3

P: n/a
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
I would like to know how to write a single bit to a file (if that's
possible).
You can only write bytes.
I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.


You may want to have a look at the 'BitArray' class.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4

P: n/a
Hi Guys,
Thanks for all the help. The way I have it right now I have a procedure
that converts a string "01001000" to a single byte and then I write that to
a file. I guess it makes sense that you can't write bit by bit, because at
some point you'd have to make sure the length was divisible by 8. The
problem being a file is always composed of the same pieces and those pieces
being some quantity of whole bytes.

Thanks for the suggestions it would definitely be faster and more memory
efficient to use a BitArray or Boolean Array. So I will try the BitArray
and if I don't like working with it then I'll just revert back to good old
Boolean :).

Also I think NiceChaps is incorrect you don't have to specify the file size
anywhere. intFile_Size = FileSystem.FileLen("C:\File.txt") can do that, it
would just be wasted space.

Thank you all very much for the help,
Christian Blackburn
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
I would like to know how to write a single bit to a file (if that's
possible).


You can only write bytes.
I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.


You may want to have a look at the 'BitArray' class.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #5

P: n/a
ROFL
Nov 20 '05 #6

P: n/a
Hi Christian,

Strictly speaking you can't even write a single byte! because the OS
writes in blocks. But it's not something we generally have to care about. :-)

Regards,
Fergus
Nov 20 '05 #7

P: n/a
Hi Christian,

Read my post carefully, I said you need to write the 'Length of Data'
NOT File Size.
Consider this situation :

Your Data is 0101010101

Your Data Length would be 10bits where as file size in Bytes would be 2
( or 16 bits!!)

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
Hi Guys,
Thanks for all the help. The way I have it right now I have a procedure
that converts a string "01001000" to a single byte and then I write that to a file. I guess it makes sense that you can't write bit by bit, because at some point you'd have to make sure the length was divisible by 8. The
problem being a file is always composed of the same pieces and those pieces being some quantity of whole bytes.

Thanks for the suggestions it would definitely be faster and more memory
efficient to use a BitArray or Boolean Array. So I will try the BitArray
and if I don't like working with it then I'll just revert back to good old
Boolean :).

Also I think NiceChaps is incorrect you don't have to specify the file size anywhere. intFile_Size = FileSystem.FileLen("C:\File.txt") can do that, it would just be wasted space.

Thank you all very much for the help,
Christian Blackburn
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
I would like to know how to write a single bit to a file (if that's
possible).


You can only write bytes.
I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.


You may want to have a look at the 'BitArray' class.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet


Nov 20 '05 #8

P: n/a
Hi Nice Chap,
Okay I'm sorry I didn't understand what you had said. Yes you're absolutely
correct. My file uses
WxHB
Where W is the Width of Squares on my table
H is the height of squares

and B is in Bits the value in each square: 0101010101010101010

So yes W * H is my data length :).
Cheers,
Christian
"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:eV**************@TK2MSFTNGP12.phx.gbl...
Hi Christian,

Read my post carefully, I said you need to write the 'Length of Data'
NOT File Size.
Consider this situation :

Your Data is 0101010101

Your Data Length would be 10bits where as file size in Bytes would be 2 ( or 16 bits!!)

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
Hi Guys,
Thanks for all the help. The way I have it right now I have a procedure
that converts a string "01001000" to a single byte and then I write that

to
a file. I guess it makes sense that you can't write bit by bit, because

at
some point you'd have to make sure the length was divisible by 8. The
problem being a file is always composed of the same pieces and those

pieces
being some quantity of whole bytes.

Thanks for the suggestions it would definitely be faster and more memory
efficient to use a BitArray or Boolean Array. So I will try the BitArray and if I don't like working with it then I'll just revert back to good old Boolean :).

Also I think NiceChaps is incorrect you don't have to specify the file

size
anywhere. intFile_Size = FileSystem.FileLen("C:\File.txt") can do that,

it
would just be wasted space.

Thank you all very much for the help,
Christian Blackburn
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
> I would like to know how to write a single bit to a file (if that's
> possible).

You can only write bytes.

> I would like to repetitiously write a 1 or 0 as the data I'm
> trying to save would dictate.

You may want to have a look at the 'BitArray' class.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet



Nov 20 '05 #9

P: n/a
Hi Fergus,
If you didn't know already those blocks are called clusters.
Cheers,
Christian

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:uw****************@TK2MSFTNGP09.phx.gbl...
Hi Christian,

Strictly speaking you can't even write a single byte! because the OS
writes in blocks. But it's not something we generally have to care about. :-)
Regards,
Fergus

Nov 20 '05 #10

P: n/a
Hi Christian,

Sectors, clusters, tracks, cylinders, surfaces, platters, drives. Yes, I
did, thanks. ;-)

And a hard-disk crash is a ClusterBomb and the result is a ClusterF*ck.

Regards,
Fergus
Nov 20 '05 #11

P: n/a
Cor

And a hard-disk crash is a ClusterBomb and the result is a

ClusterF*ck.

My English is not so good, will that give the nicest result a clusterfind
check?
Nov 20 '05 #12

P: n/a
Hi Cor,

|| will that give the nicest result a clusterfind check?

??

Regards,
Fergus
Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.