Results 1 to 10 of 10

Thread: [Resolved] Search a BYTE Array

  1. #1

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    [Resolved] Search a BYTE Array

    Is there a function that can search a byte array for a series of bytes or a string?
    Last edited by randem; Jun 30th, 2005 at 01:30 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Search a BYTE Array

    Don't think so, I suppose you could turn it into a string somehow and use InStr but otherwise I would use a loop and search every so many bytes for the sequence you're looking for.

  3. #3

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Search a BYTE Array

    penagate,

    Thanks, StrConv seems to only like text and my array can contain anything. I guess I will have to write one.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Search a BYTE Array

    Maybe you could do this?
    VB Code:
    1. Dim strTemp As String
    2. strTemp = Space(UBound(ByteArray))
    3. CopyMemory ByVal strTemp, ByteArray(0), UBound(ByteArray)
    Although since strings are wide I'm not sure if that would work, you might have to insert zero bytes at every odd interval.

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search a BYTE Array

    Here:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim A(-1 To 10) As Byte, B(5 To 7) As Byte
    5.    
    6.     A(-1) = 100
    7.     A(0) = 5
    8.     A(1) = 6
    9.     A(2) = 67
    10.     A(3) = 3
    11.     A(4) = 34
    12.     A(5) = 76
    13.     A(6) = 12
    14.     A(7) = 34
    15.     A(8) = 2
    16.     A(9) = 1
    17.     A(10) = 99
    18.    
    19.     B(5) = 12
    20.     B(6) = 34
    21.     B(7) = 2
    22.    
    23.     Debug.Print SearchByteArray(A, B, -888)
    24. End Sub
    25.  
    26. Public Function SearchByteArray(BigArr() As Byte, SearchArr() As Byte, Optional ByVal NotFoundRet As Long = -1) As Long
    27.     Dim K As Long, Q As Long, LB As Long
    28.    
    29.     LB = LBound(SearchArr)
    30.    
    31.     For K = LBound(BigArr) To UBound(BigArr) - (UBound(SearchArr) - LB)
    32.         For Q = LB To UBound(SearchArr)
    33.             If BigArr(K + Q - LB) <> SearchArr(Q) Then Exit For
    34.         Next Q
    35.        
    36.         If Q = UBound(SearchArr) + 1 Then Exit For
    37.     Next K
    38.    
    39.     If K <= UBound(BigArr) - (UBound(SearchArr) - LB) Then
    40.         SearchByteArray = K
    41.     Else
    42.         SearchByteArray = NotFoundRet
    43.     End If
    44. End Function
    Should I put this in the CodeBank ? i think I should right ?
    Last edited by CVMichael; Jun 12th, 2005 at 02:15 AM.

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search a BYTE Array

    By the way... when you do InStr in VB, in the background, that's how it does the search...

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Search a BYTE Array

    I thought as much, but its written in C or something, so it's a fair bit quicker than anything we could write ourselves in VB.

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search a BYTE Array

    Quote Originally Posted by penagate
    I thought as much, but its written in C or something, so it's a fair bit quicker than anything we could write ourselves in VB.
    YUP

  9. #9

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Search a BYTE Array

    I wrote the routine but it if god-awful slow. It is not an option.

  10. #10
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Search a BYTE Array

    Quote Originally Posted by randem
    I wrote the routine but it if god-awful slow. It is not an option.
    Did you try CVMichael's code a few posts back?

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

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