-
Mar 4th, 2012, 12:54 PM
#1
Thread Starter
Junior Member
[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
-
Mar 4th, 2012, 02:02 PM
#2
Addicted Member
-
Mar 4th, 2012, 02:12 PM
#3
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
-
Mar 4th, 2012, 02:34 PM
#4
Thread Starter
Junior Member
Re: Help with Function
Originally Posted by vb_ftw
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.
-
Mar 4th, 2012, 02:51 PM
#5
Re: Help with Function
This is the line that is causing it to stop after the first match
Code:
If Matched Then Return I
-
Mar 4th, 2012, 03:59 PM
#6
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
-
Mar 4th, 2012, 04:32 PM
#7
Thread Starter
Junior Member
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.
-
Mar 4th, 2012, 05:34 PM
#8
Re: Help with Function
Like this:-
vbnet Code:
For Each i As Integer In ByteMatch(src, match) ListBox1.Items.Add(i) Next
-
Mar 4th, 2012, 08:56 PM
#9
Re: Help with Function
Is there an Items.AddRange?
My usual boring signature: Nothing
-
Mar 4th, 2012, 11:17 PM
#10
Re: Help with Function
Ya, I tried that but AddRange takes a ListBox.ObjectCollection object and not an array as you would expect.
-
Mar 5th, 2012, 10:01 AM
#11
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|