1

在 VB.Net 中,我使用 BinaryReader 打开一个文件。

我需要在文件中找到一些十六进制值,如果找到,它会返回第一个字节的偏移地址。

有可能的?怎么能做到这一点?谢谢

编辑:

我当前的代码:

Private Function findOffset()
    Using reader As New BinaryReader(File.Open(filename, FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0 ' <== THIS IS THE OFFSET
        Dim length As Integer = reader.BaseStream.Length
        Do While pos < length
            ' Read the integer.
            Dim value As Byte = reader.ReadByte()
            If value = CByte(&H41) Then
                Return pos
                Exit Do
            End If
            ' Add length of integer in bytes to position.
            pos += 1
        Loop
    End Using
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(Hex(findOffset()).ToString.PadLeft(6, "0"c))
End Sub

我想做的是:

例如,我打开一个文件,在用十六进制编辑器打开的文件中,我看到有一些十六进制值,41,42,43,44. 我需要找到该值,然后返回找到它们的偏移地址。

使用我当前的代码它可以工作,但我只能找到 1Byte,而且我需要找到超过 1 个 .. 我可能需要找到 1kb 或更多的数据!

我这样做是为了在一些 bin 文件中找到可用空间。因此,例如我需要 10Byte 的可用空间。那将FF,FF,FF,FF,FF,FF,FF,FF,FF,FF在一个 Hex heditor 中,我需要找到它并返回第一个空字节的偏移地址。

编辑 2

这里是第一行代码。

Private Function findOffset(query as Byte())
        Using reader As New BinaryReader(File.Open(filename, FileMode.Open))
            Dim startOffset = 80
            Dim length As Integer = reader.BaseStream.Length - startOffset
            reader.BaseStream.Position = startOffset
            If query.Length <= length Then
            ...

但不起作用..它告诉我可用空间从十进制偏移量开始00000047

我在这里做错了,我不太明白你的意思

修改“length”变量“length = length - startOffset”

4

1 回答 1

1
Using reader As New BinaryReader(File.Open("file.bin", FileMode.Open))
    ' Loop through length of file.
    Dim pos As Integer = 0 ' <== THIS IS THE OFFSET
    Dim length As Integer = reader.BaseStream.Length
    While pos < length
    ' Read the integer.
    Dim value As Byte = reader.ReadByte()
            If value == 123 Then
                return pos
            End If
    ' Write to screen.
    Console.WriteLine(value)
    ' Add length of integer in bytes to position.
    pos += 1
    End While
End Using

从http://www.dotnetperls.com/binaryreader-vbnet稍作修改

编辑要搜索值数组,您必须在函数内部循环遍历该数组。

Private Function findOffset(query as Byte())
    Using reader As New BinaryReader(File.Open(filename, FileMode.Open))
        Dim length As Integer = reader.BaseStream.Length
        If query.Length <= length Then
            ' process initial (first) search
            Dim values As Byte() = reader.ReadBytes(query.Length)
            Dim found As Boolean = False
            For fnd = 0 To query.Length - 1
                If values(fnd) <> query(fnd) Then
                    found = False
                    Exit For
                End If
                found = True
            Next fnd
            If found = True Then 
                Return 0
            Else ' keep searching the rest of the binary stream
                For pos = query.Length To length - 1
                    ' shift values over 1, [1,2,3,4] => [2,3,4,4]
                    Array.Copy(values, 1, values, 0, values.Length - 1)
                    ' put the next new byte at the end
                    values(values.Length - 1) = reader.ReadByte()
                    For fnd = 0 To query.Length - 1
                        If values(fnd) <> query(fnd) Then
                            found = False
                            Exit For
                        End If
                        found = True
                    Next fnd
                    If found = True Then
                        Return pos - (query.Length - 1)
                    End If
                Next pos
            End If
        End If
    End Using
    Return -1 ' not found
End Function

注意:我没有测试上面的代码,所以可能有语法错误。

于 2012-11-13T14:08:52.873 回答