Results 1 to 3 of 3

Thread: Return Offsets of Hex String in file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Return Offsets of Hex String in file

    Greetings. Is there a possible way to return the offset of Hex that is being searched for. For example, I need to find the location of all occurances of : "10223344DDADDD" in the file and paste their offsets in a listbox. I have a way of searching bytes but it involves using {&H10,&H22,&H33...} but that is complicated for the application that I need it for... It needs to be as I've noted above... Any advice?Name:  test.jpg
Views: 124
Size:  37.9 KB

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    7,596

    Re: Return Offsets of Hex String in file

    You could write something like this:-
    Code:
        Private Function FindOffsets(ByVal filePath As String, ByVal findHex As String) As Long()
    
            Dim bytes = (From i In Enumerable.Range(0, findHex.Length \ 2)
                         Select Convert.ToByte(findHex.Substring(i * 2, 2), 16)).ToArray
            Return FindOffsets(filePath, bytes)
        End Function
    
        Private Function FindOffsets(ByVal filePath As String, ByVal find As Byte()) As Long()
            Dim firstByte As Byte = find(0)
            Dim offsets As New List(Of Long)
    
            find = find.Skip(1).ToArray
    
            Using br = New BinaryReader(IO.File.OpenRead(filePath))
                Dim b As Byte()
    
                b = br.ReadBytes(1)
    
                Do Until b.Length = 0
                    If b(0) = firstByte Then
                        b = br.ReadBytes(find.Length)
    
                        If b.SequenceEqual(find) Then offsets.Add(br.BaseStream.Position - (find.Length + 1))
                    End If
                    b = br.ReadBytes(1)
                Loop
    
    
            End Using
    
            Return offsets.ToArray
        End Function
    And use it like this:-
    Code:
    Dim offsets = FindOffsets("d:\testfile2.bin", "10223344DDADDD")
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Return Offsets of Hex String in file

    Working great! Thank you for a fast and accurate reply!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width