VB.Net获取偏移地址(VB.Net Get Offset Address)

2019-10-17 09:24发布

在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 。 我需要找到一个值,然后返回他们被发现的偏移地址。

随着我当前的代码它的工作原理,但我只能找到1字节,我需要找到超过1。5月我需要查找数据或更多的1KB!

我在做这部分的bin文件查找可用空间。 因此,例如,我需要的自由空间10Byte。 这将是FF,FF,FF,FF,FF,FF,FF,FF,FF,FF十六进制heditor里面,我需要找到并返回第一个空字节的偏移ADDRES。

编辑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

我做错了什么在这里,我没有很好地理解你的意思是什么

修改“长度”可变“长度=长度 - 开始偏移”

Answer 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

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



文章来源: VB.Net Get Offset Address