VB.Net获取偏移地址 [英] VB.Net Get Offset Address

查看:422
本文介绍了VB.Net获取偏移地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

编辑:



我现在的代码:

 私人函数findOffset()
使用reader新BinaryReader(File.Open(filename,FileMode.Open))
'循环遍历文件的长度。
Dim pos As Integer = 0'< ==这是偏移
Dim length As Integer = reader.BaseStream.Length
Do While pos<长度
'读取整数。
Dim value As Byte = reader.ReadByte()
如果value = CByte(& H41)然后
返回位置
退出
结束如果
'以字节为单位向位置添加整数长度。
pos + = 1
Loop
End使用
End Function

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

我想要做的是:



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



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



我正在使用它来查找某些bin文件中的可用空间。所以例如我需要10Byte的可用空间。这将是一个十六进制heditor内的 FF,FF,FF,FF,FF,FF,FF,FF,FF,FF ,我需要找到并返回偏移量第一个空白字节的地址。



编辑2



pre $ 私人函数findOffset(查询为Byte())
使用reader作为新的BinaryReader(File.Open (filename,FileMode.Open))
Dim startOffset = 80
Dim length As Integer = reader.BaseStream.Length - startOffset
reader.BaseStream.Position = startOffset
如果查询。长度< =长度然后
...

但不起作用..它告诉我可用空间从小数点开始偏移 00000047



我在这里做错了什么,我不太明白b
$ b


修改length变量length = length - startOffset



使用reader作为新BinaryReader(File.Open(file.bin,FileMode.Open))
'循环遍历文件的长度。
Dim pos As Integer = 0'< ==这是偏移
Dim length As Integer = reader.BaseStream.Length
虽然pos<长度
'读取整数。
Dim value As Byte = reader.ReadByte()
If value == 123 Then
return pos
End If
'写入屏幕。
Console.WriteLine(value)
'将整数长度以字节为单位添加到位置。
pos + = 1
End While
End使用

稍微从 http://www.dotnetperls.com/binaryreader-vbnet 进行修改



编辑要搜索一个值的数组,您必须在函数内循环访问该数组。



<$
使用阅读器作为新的BinaryReader(File.Open(filename,FileMode.Open))
Dim length As Integer = reader.BaseStream.Length
如果query.Length< = length然后
'处理初始(第一次)搜索
Dim values As Byte()= reader.ReadBytes(query.Length)
Dim found As Boolean = False
For fnd = 0 To query.Length - 1
如果values(fnd)< query(fnd)然后
found = False
退出对于
结束如果
找到= True
下一个fnd
如果找到= True然后
返回0
否则'继续搜索二进制流的其余部分
对于pos = query.Length要长度 - 1
'的值超过1,[1,2,3,4] = > [2,3,4,4]
Array.Copy(values,1,values,0,values.Length - 1)
'在结尾放置下一个新字节
values( values.Length - 1)= reader.ReadByte()
For fnd = 0 To query.Length - 1
如果values(fnd)< query(fnd)然后
found = False
退出对于
结束如果
找到= True
下一个fnd
如果找到= True然后
返回pos - (query.Length - 1)
End If
Next pos
End If
End If
End使用
返回-1'not found
End Function

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


In VB.Net I open a file with BinaryReader..

I need to find on the file for some Hex values and if they are found, it return the offset address of the first Byte..

It is possible? and how can achieve this? Thank you

EDIT:

My current code:

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

What i'm trying to do is:

For example i open a file and in that file opened with a Hex editor i see there are some Hex values, 41,42,43,44. I need to Find that values and then return the Offset address where they are found.

With my current code it works, but i can only find 1Byte, and i need to find more than 1.. May i need to find 1kb of data or more!

I'm making this to find free space in some bin files. So for example i need 10Byte of free space. that would be FF,FF,FF,FF,FF,FF,FF,FF,FF,FF inside a Hex heditor, and i need to find that and return the offset addres of the first empty byte.

EDIT 2

Here the first lines of code.

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
            ...

But doesn't work.. It tell me that free space start from decimal offset 00000047

I did something wrong here, i didn't understand well what you mean by

modify the "length" variable "length = length - startOffset"

解决方案

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

Slightly modified from http://www.dotnetperls.com/binaryreader-vbnet

EDIT To search for an array of values you have to loop through that array inside of your function.

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

Note: I haven't tested the above code so there may be syntax errors.

这篇关于VB.Net获取偏移地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆