Solved

How to change a specific bit from a file

Posted on 2016-11-09
2
40 Views
Last Modified: 2016-11-10
Hi.

Anyone know how to read a file (binary file) and change a specific bit and then write the file back in vb.net?

I'm trying to change mp3 header bit. The samples I've seen only show with text file.
0
Comment
Question by:MCSI Developers
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41881182
Hi MCSI Developers;

The following will show you how to modify a file in binary mode.
Imports System.IO

'' File to be opened
Dim filename = "mpthreetest.mp3"
'' Set up array to read the bytes from the file system into memory
Dim readFromFile() As Byte
'' Used to hold the file size
Dim filesize As Long

'' Read the file into the array the FileStream
Using fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    '' Reading the file with a binary reader
    Using br As New BinaryReader(fs)
        '' Get the size of the file
        filesize = br.BaseStream.Length - 1
        '' Create the array with the file size elements
        readFromFile = New Byte(filesize) {}
        '' Make sure the we are at the begining of the file
        br.BaseStream.Seek(0, SeekOrigin.Begin)
        '' Read all bytes into array
        br.Read(readFromFile, 0, filesize)
    End Using
End Using

'' Modify the array at the position needed
'' this statement will change the value of the 9th byte of the file 
'' to a decimal 9. Note I am assigning the value in hex
readFromFile(8) = &H39

'' After modifyinf the array write all bytes back to the file
Using fs As New FileStream(filename, FileMode.Open, FileAccess.Write)
    Using bw As New BinaryWriter(fs)
        bw.Write(readFromFile)
    End Using
End Using

Open in new window

1
 
LVL 1

Author Comment

by:MCSI Developers
ID: 41882128
Thanks, I've used VB5.0 to do this.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question