Find and replace in binary

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
3 posts Page 1 of 1
Contributors
User avatar
shawty220
Just Registered
Just Registered
Posts: 9
Joined: Fri Jun 18, 2010 3:31 pm

Find and replace in binary
shawty220
I have the code to read binary etc.
I want to find a replace a text string in a binary, any code?
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Find and replace in binary
mandai
What sort of string? This should work for ASCII. Also assuming your input is a byte array.
Code: Select all
        Dim input As Byte() = Encoding.ASCII.GetBytes("00FindThis12")

        Dim lookFor As Byte() = Encoding.ASCII.GetBytes("FindThis")

        Dim temp(lookFor.Length - 1) As Byte

        For i As Integer = 0 To input.Length - lookFor.Length
            System.Buffer.BlockCopy(input, i, temp, 0, lookFor.Length)
            'MsgBox(Encoding.ASCII.GetString(temp))'show progress

            If temp.SequenceEqual(lookFor) Then
                MsgBox("Found lookfor at offset " & i & " of input.")
                input(i) = Asc("2"c) ' replace 'F' with '2'
                MsgBox(Encoding.ASCII.GetString(input))'output for visible purpose only. do not use this string for your data
                Exit For
            End If
        Next
User avatar
shawty220
Just Registered
Just Registered
Posts: 9
Joined: Fri Jun 18, 2010 3:31 pm

Re: Find and replace in binary
shawty220
That should work great thank you :)
3 posts Page 1 of 1
Return to “Coding Help & Support”