Reputation: 2440
In Excel VBA I'm messing around with reading and writing binary files.
I was wondering if it is possible to instead of only changing the bytes to actually work with the bits. I don't mean taking each byte and converting it to 8 bits, I mean if it is possible to create/modify/read files with bit control where it might be, for example, a file with only 4 bits, or 10, instead of always being multiple of 8.
Here's some of the code that I'm using:
Private Sub WriteFile()
Dim path As String, num As Byte, arr(2) As Byte
path = "test.bin"
arr(0) = CByte(&HA1)
arr(1) = CByte(&HC4)
arr(2) = CByte(&HA1)
num = FreeFile
If Dir(path) <> "" Then Kill path
Open path For Binary Access Write As num
Put num, , arr
Close num
End Sub
Upvotes: 2
Views: 598
Reputation: 57683
Well the question is why would you do that? I think since the smallest I/O unit of a file is 1 byte (8 bits) you will always need to write 1 full byte.
The approach would be to generate a bit stream and convert that into bytes (just before writing the file).
So the approach to change one bit would be read 1 full byte change the bit in that byte and write 1 full byte back to the file.
You won't save any space on disk anyway because you always need to write 1 full sector to the disk and typically 1 sector = 512 bytes. So any file below 512 bytes will always use the full 512 bytes on the disk.
Upvotes: 4