Results 1 to 11 of 11

Thread: [RESOLVED] Help with Function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    16

    Resolved [RESOLVED] Help with Function

    I found this code and I think it could be useful for some members. The code search for the specified bytes in jpg file and when first match is founded msgbox gives you position of the bytes in the file and Function stops.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim B() As Byte = My.Computer.FileSystem.ReadAllBytes("C:\users\Public\SIMG1374.jpg")
            Dim C() As Byte = {164, 114, 78, 88, 245, 164, 83, 158, 43}
            Dim P As Integer = ByteMatch(B, C)
            If P = -1 Then MsgBox("No Match") Else MsgBox("Matched at " & P.ToString)
        End Sub
        Function ByteMatch(ByVal Source As Byte(), ByVal Match As Byte()) As Integer
            For I As Integer = 0 To Source.Count - 1
                Dim Matched As Boolean = False
                If Match(0) = Source(I) Then
                    For J As Integer = 1 To Match.Count - 1
                        Matched = True
                        If Match(J) <> Source(I + J) Then
                            Matched = False
                            Exit For
                        End If
                        If Matched Then Return I
                        Exit For
                    Next J
                End If
            Next I
            Return -1
        End Function
    End Class

    Now, because I am newbie to VB I have one question. How can I change the Function so that Function search the whole file for all byte matches ?
    Thanks in advance

  2. #2
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Help with Function

    it already does that.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Help with Function

    Not quite, it will stop when it finds a match. I would suggest adding a third input, which is startingPoint. Then change this line:

    For I As Integer = 0 To Source.Count - 1

    to

    For I As Integer = startingPoint To Source.Count - 1

    Then, you start it out by passing in 0 as the startingPoint, and if it returns something other than -1, you call the method again passing in that number +1 as the new startingPoint.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    16

    Re: Help with Function

    Quote Originally Posted by vb_ftw View Post
    it already does that.
    I've tested the code and as I already said when function finds the first match it stops so if file (jpg in this case) contains several byte arrays - C() - you're searching for (say 4) msgbox(s) will display only position of first match not all 4 of them.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,173

    Re: Help with Function

    This is the line that is causing it to stop after the first match
    Code:
    If Matched Then Return I

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Help with Function

    You can either solve it the way I suggested, or you can change it to return a List (of Integer), and rather than calling Return I, you would simply add I to the list, then at the end of the function return the list. It would look like this:
    Code:
     Function ByteMatch(ByVal Source As Byte(), ByVal Match As Byte()) As List (of Integer)
            Dim ret As New List(of Integer)
            For I As Integer = 0 To Source.Count - 1
                Dim Matched As Boolean = False
                If Match(0) = Source(I) Then
                    For J As Integer = 1 To Match.Count - 1
                        Matched = True
                        If Match(J) <> Source(I + J) Then
                            Matched = False
                            Exit For
                        End If
                        If Matched Then 
                         ret.Add(I)
                        End If
                    Next J
                End If
            Next I
            Return ret
        End Function
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    16

    Re: Help with Function

    Thank you for your replies Shaggy but I still didn't make it work. Is it possible to add Function result you provided to ListBox and how (if possible)? I would be very grateful if you can provide me some code with StartingPoint you're talking about in your post.
    Thanks.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,683

    Re: Help with Function

    Like this:-
    vbnet Code:
    1. For Each i As Integer In ByteMatch(src, match)
    2.             ListBox1.Items.Add(i)
    3.         Next

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Help with Function

    Is there an Items.AddRange?
    My usual boring signature: Nothing

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,683

    Re: Help with Function

    Ya, I tried that but AddRange takes a ListBox.ObjectCollection object and not an array as you would expect.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    16

    Re: Help with Function

    I have managed to display Function result in Listbox. Let me just say when I run the code I got duplicate values in the listbox so I've added a Sub to remove duplicates from the listbox and finally I have what I was looking for.
    Thank you for helping me solve this problem.

Tags for this Thread

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