Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to change a specific bit from a file

Posted on 2016-11-09
2
Medium Priority
?
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

718 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