Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to change a specific bit from a file

Posted on 2016-11-09
2
Medium Priority
?
63 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:Mlungisi Ndlela
2 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 2

Author Comment

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

972 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