CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Posts
    135

    Can you search a array for another array?

    I need to search a Byte array for the existence of another Byte array. The first array is large, about 30,000 bytes and the second varies but is usually between 300 and 500 bytes. I need to know the start position of the second array inside the first. For example if these were the arrays:
    Code:
    Array 1     Array 2
    00
    34
    00
    23              23
    00              00
    64              64
    00              00
    41              41
    00
    45
    92
    33
    I would need to know the start position is 3. I COULD do something horribly painful like:
    Code:
            For x = 0 To UBound(Array1)
                If Array1(x) = Array2(0) And Array1(x + 1) = Array2(1) And Array1(x + 2) = Array2(2) Then
                    ' Found it
                End If
            Next
    And loop through finding it by brute force but array 2 will be a different size on each system so doing the above, although I could make it work, isn't practical. I could even do multiple if's like if I find the first byte then find the second, then find the third, and make it a little more bearable but still. Is there a simple way to find a array inside another array?

    -Allan

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Can you search a array for another array?

    I would think that you could copy the array into a string variable and use instr() to locate the position you seek. Not sure how fast it would be but it should work. Of course there may very well be a better approach.

  3. #3
    Join Date
    Jul 2003
    Posts
    135

    Re: Can you search a array for another array?

    yeah, I actually have done that I just hate converting from one type to another then back. It's really not that much slower, doing a simple:

    MyPosition = InStr(myFirstArrayAsString, MySecondArrayAsString)

    But then I need to figure out the position difference when I do this compared to doing the byte array directly. If I have to though this is what I'll end up doing, I just through that finding a array inside a array was possible somehow.

  4. #4
    Join Date
    Jul 2003
    Posts
    135

    Re: Can you search a array for another array?

    I'm going to have to use the extended if then for this. I converted my byte arrays into strings then because the array is actually in hex (yet the read registry function pulls in decimal) I had to covert each value to hex before adding to the string (so every two positions is one byte so I can use the position later). I can get the full byte array as a hex string however:

    Code:
    myRegistryKeyAsByte = ReadCURKByte("Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify", "IconStreams", Nothing)
                    For x = 0 To UBound(myRegistryKeyAsByte)
                        myHolderString = double_to_hex(myRegistryKeyAsByte(x))
                        Select Case myHolderString.Length
                            Case 0
                                myRegistryKeyAsString += "00"
                            Case 1
                                myRegistryKeyAsString += "0" + myHolderString
                            Case 2
                                myRegistryKeyAsString += myHolderString
                        End Select
                    Next
    But for some reason a InStr function will NOT work on the two strings. If I take the values and past my registry string (from above) into Notepad and then search using the string I'm looking for it does find it. But the VB InStr does not. Maybe it can't deal with searching a 36,000 character string for a 500 character string. Not sure, gonna keep playing.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Can you search a array for another array?

    Could it be a case sensitivity issue.

    There should only be a position difference of 1 in the value returned by instr vs the actual array location. e.g. first array element is 0 instr() will return 1 for the first position so it should just be a matter of subtracting one from the result of the instr() if you choose to go that route.

  6. #6
    Join Date
    Apr 2008
    Posts
    82

    Re: Can you search a array for another array?

    This isn't complete, but it should give you some ideas. You will need to check bounds

    Code:
            Dim rDataGen As New Random
            Dim bBuf(9999) As Byte
            Dim fndA() As Byte = New Byte() {1, 2}
            Dim loc As Integer
            Dim found As Boolean
            rDataGen.NextBytes(bBuf) 'generate some test data
            loc = 0
            Do
                loc = Array.IndexOf(bBuf, fndA(0), loc) 'find the first byte
                If loc <> -1 Then 'then check the rest
                    found = True
                    For x As Integer = 1 To fndA.Length - 1
                        loc += 1
                        If bBuf(loc) <> fndA(x) Then
                            found = False
                            Exit For
                        End If
                    Next
                    If found Then Exit Do
                End If
            Loop

  7. #7
    Join Date
    Jul 2003
    Posts
    135

    Re: Can you search a array for another array?

    Thanks for the example. I got my goofy "switch byte array to hex string" example above working and I did find the string within the string and figured out how to then go back and change the byte I needed (it was always 10 bytes in front of the first character of my string, or 20 spaces in string format).

    I think I am giving up on it though....I've been working on this to solve this solution: http://www.codeguru.com/forum/showthread.php?t=306661 which with this done I think I have solved. Problem is solving the problem programmaticly is almost not worth doing to get through the hoops to do it.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Can you search a array for another array?

    If you use a List, you can use FIND for the first value, then skip to the end or something
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured