Results 1 to 3 of 3

Thread: VB.net code help - reading, writing & accessing bits of a binary file

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    Thumbs up VB.net code help - reading, writing & accessing bits of a binary file

    Following is a code that I'm still working on. I searched over internet so hard, that I could find was methods such as bitshifting, bitarray, binarywriter, streamwriter etc. But I couldn't find a clear codes to tailor the specific needs.

    What I'm trying to do is access a binary file, read, write append data to that file. But "IO.BinaryWriter" command just read & write "bytes" not "bits" as I see. I'm trying to build a file compressor program by replacing 5 bits to other predefined 3 bits to reduce the file size. I need to know certain codes to do that which I couldn't find clear codes for what I'm trying to do. I have attached .jpg image what I need in the code. Please go through it

    see this image http://i58.tinypic.com/2zdzy2d.png

    and please be kind enough to suggest me codes. & can this be done via "streamWriter" for better performance (some codes are better than others etc) and speed as this is a compression program it needs speed and performance. Really big help to me.

    ** the main things I need to do is
    1) load a file (say 400 MB) into the computer memory as bytes or bits
    2) replace bits in specific location in the file (0 --> 1 etc) (in computer memory)
    3) delete bits in specific locations
    3) getting file size in bits after deleting the particular bits

    other things are written in the image file provided above

    Many Thanks

    Code:
    Public Class Form1
    
        Dim filename As String = "D:\temp\binfile.bin"
        Dim writer As IO.BinaryWriter
        Dim reader As IO.BinaryReader
        Dim tmpByteData As Byte
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            writer = New IO.BinaryWriter(IO.File.Open(filename, IO.FileMode.Append))
            Using writer
    
                writer.Write(10001)   'byte
    
               End Using
            writer.Close()
    
            If (IO.File.Exists(filename)) Then
                reader = New IO.BinaryReader(IO.File.Open(filename, IO.FileMode.Open))
                Using reader
    
                    tmpByteData = reader.ReadByte()
    
                End Using
                reader.Close()
            End If
    
    
    
        End Sub
    End Class

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: VB.net code help - reading, writing & accessing bits of a binary file

    You can only read and write bytes to a file, so you are going to have to do the bit manipulation in memory.
    Perhaps you might want to read the bytes from the file, convert the byte array to a BitArray type, manipulate your bits, then convert the BitArray back to a byte array and write it to the disk.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB.net code help - reading, writing & accessing bits of a binary file

    To go a bit further: The memory system on all modern computers can only address memory down to the byte size, so it isn't possible to get less than that in a single read, since the system can have the address of a byte, but can't have the address of anything less than a byte.

    In fact, I doubt that any modern system reads as little as a byte, either, but just pretends that it does. When you ask for byte X, you are probably getting X along with a bunch of other bytes adjacent to X into the cache. All that doesn't matter, though, as what you see is X, and the rest is just done for overall efficiency. Nonetheless, you can't say "give me the first bit of X", because the CPU would have to do exactly what you'd have to do, which is to get X and mask off the high bits.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width