Best way to search an array for a value and return offset position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • King96
    New Member
    • Oct 2014
    • 3

    Best way to search an array for a value and return offset position

    I have a binary file loaded into a byte array (which might need to be converted to a string). I am trying to enter a value in Txtbx2 and search the array, when found return the offset where it was found. Is there a way I can alter the code below to work or is there another way?

    Code:
    Dim [Data] As Byte() = New Byte() {1, 2, 4, 6, 3}
            Dim find As Byte = TextBox2.Text
            Dim index As Integer = System.Array.IndexOf(Of Byte)([Data], find)
    
            If (index = -1) Then
                MessageBox.Show("Not Found")
                'Not found...
            Else
                'Found
                TextBox3.Text = index
            End If
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Are you trying to find a byte array or an individual byte? or what are you trying to accomplish really?

    Comment

    • King96
      New Member
      • Oct 2014
      • 3

      #3
      Im trying to search a byte array for a particular byte and then return the offset where it is located and do so for every instance that specific byte is used in a binary file

      Comment

      • onlinetuition
        New Member
        • Oct 2014
        • 10

        #4
        If you want to search many times, you may sort it and perform binary search.

        Comment

        • King96
          New Member
          • Oct 2014
          • 3

          #5
          I figured out how to return the decimal offset or block. Now I have to figure out how to loop it to continue searching, and how to properly print the output

          Comment

          Working...