Solved

How to change a specific bit from a file

Posted on 2016-11-09
2
20 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 62

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now