Results 1 to 4 of 4

Thread: Find String inside of a Byte array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Find String inside of a Byte array

    How do you search for a string inside of a byte array? I know it is there, but the sizeIndex always comes up as -1. I looked at the bytes and they are at the very beginning, but it can not find them. I would like to know the position of the data in the array.

    Code:
    vb Code:
    1. Dim theBytes As Byte() = ASCII.GetBytes("ENDSIZE".ToArray)
    2. Dim sizeIndex As Integer = Array.IndexOf(data, theBytes)

    theBytes = {Length=7}

    data = {Length=47465}
    Beginning of data:
    ENDSIZE;ScreenWidth+1376+ScreenHeight
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Find String inside of a Byte array

    Why exactly are you looking for text in a Byte array? Would it not make more sense to search for text in a String?

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Find String inside of a Byte array

    Quote Originally Posted by rex64 View Post
    How do you search for a string inside of a byte array? I know it is there, but the sizeIndex always comes up as -1. I looked at the bytes and they are at the very beginning, but it can not find them. I would like to know the position of the data in the array.

    Code:
    vb Code:
    1. Dim theBytes As Byte() = ASCII.GetBytes("ENDSIZE".ToArray)
    2. Dim sizeIndex As Integer = Array.IndexOf(data, theBytes)

    theBytes = {Length=7}

    data = {Length=47465}
    Beginning of data:
    ENDSIZE;ScreenWidth+1376+ScreenHeight
    Try this:
    Code:
    Option Compare Binary
    Option Explicit On
    Option Infer Off
    Option Strict On
    
    Imports System
    Imports System.Text
    
    Public Module Module1
    
       Public Sub Main()
          Dim Bytes() As Byte = {}
          Dim Found As Boolean = Nothing
          Dim SearchText() As Byte = {}
          Dim UnicodeEncodingO As New UnicodeEncoding
    
          Bytes = UnicodeEncodingO.GetBytes("This is the byte array to be searched.")
          SearchText = UnicodeEncodingO.GetBytes("array")
          For Index1 As Integer = Bytes.GetLowerBound(0) To Bytes.GetUpperBound(0) - (SearchText.GetUpperBound(0) - SearchText.GetLowerBound(0))
             Found = True
             For Index2 As Integer = SearchText.GetLowerBound(0) To SearchText.GetUpperBound(0)
                If Not Bytes(Index1 + Index2) = SearchText(Index2) Then
                   Found = False
                   Exit For
                End If
             Next Index2
             If Found Then
                Console.WriteLine("Found at: {0}", Index1)
                Exit For
             End If
          Next Index1
       End Sub
    End Module

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Find String inside of a Byte array

    This is for parsing binary data received over the network. I guess the best option would be to re-structure our network code, and I think we may have done that in newer programs. It would still be cool to have a fast search for string inside of a byte array though
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width