Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:12:45+00:00 2026-06-15T01:12:45+00:00

In VB.Net I open a file with BinaryReader.. I need to find on the

  • 0

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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:12:46+00:00Added an answer on June 15, 2026 at 1:12 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First some background, I've written an open source .NET library, named Duplicitiy (on github.com),
Some file(file.dat) #####Start#### sometext sometext From: email@address.net some text some text ####End##### 1 import
Through an C#/ASP.NET website, I'm using SpreadsheetGear to open a file from a template,
failed to open file file://D/:/dev/test_all.html JavaException: java.net.UnknownHostException: D Any ideas for why this happens?
I need to read in a rolling log file in .NET 3.5sp1. I'm wondering
I want to open file for append on Windows 7 host using C#/.NET. I
Possible Duplicate: Wait until file is unlocked in .NET I have an open file,
I'm using Asp.net to create a csv file that the user can open directly
Is it possible to select the whole folder in VB.NET open dialog box? By
The respected open source .NET wrapper implementation ( SharpBITS ) of Windows BITS services

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.