-
Aug 12th, 2018, 05:08 PM
#1
Thread Starter
Member
Find 3 Bytes in Byte Array
Hi all
i am loading a File into a Byte Array with
Dim bytes As Byte() = My.Computer.FileSystem.ReadAllBytes(lstfilepaths.SelectedItem)
after that i`d like to search inside ths Byte Array 3 Bytes (e.g. 20FFBD) and return the exact Position inside the Array. It may happen that the 3 Bytes are available multiple Times inside this Array,so after the Array has been found the first Time i Need to continue searching until end of the Array. I want to return the Byte positions inside a TextBox to verify if there was a match.
Also if the Bytes have been found i Need to replace theese 3 Bytes with 3 other Bytes in all found Locations.
Can somebody please help me out there please ?
Thans in Advance
-
Aug 12th, 2018, 05:18 PM
#2
Re: Find 3 Bytes in Byte Array
What part of this are you having a problem with? What code have you written so far?
Please don't expect to post what looks like a homework assignment and then come back an hour later hoping that someone has posted fully functional code that does what you've asked.
-
Aug 12th, 2018, 05:30 PM
#3
Thread Starter
Member
Re: Find 3 Bytes in Byte Array
Originally Posted by OptionBase1
What part of this are you having a problem with? What code have you written so far?
Please don't expect to post what looks like a homework assignment and then come back an hour later hoping that someone has posted fully functional code that does what you've asked.
I dont expect a one Hour Solution at all. I`ve been searching the net for small Code examples that would search for atleast 2 Bytes inside an Byte Array but i couldnt find one.
Current Code is actually , select a Folder where my Files are, then list them in a Listbox, I have a "small" analyzing Logic that reads the first 2 Bytes from each File as i Need this for a start Offset on another Computer System.
Concering the Byte search inside the Array i actually have absolutely Nothing since i have no idea how to search inside the bytearray
So searching the Bytes and the Position inside the Array is the biggest problem atm
-
Aug 12th, 2018, 05:31 PM
#4
Thread Starter
Member
Re: Find 3 Bytes in Byte Array
Originally Posted by OptionBase1
What part of this are you having a problem with? What code have you written so far?
Please don't expect to post what looks like a homework assignment and then come back an hour later hoping that someone has posted fully functional code that does what you've asked.
I dont expect a one Hour Solution at all. I`ve been searching the net for small Code examples that would search for atleast 2 Bytes inside an Byte Array but i couldnt find one.
Current Code is actually , select a Folder where my Files are, then list them in a Listbox, I have a "small" analyzing Logic that reads the first 2 Bytes from each File as i Need this for a start Offset on another Computer System.
Concering the Byte search inside the Array i actually have absolutely Nothing since i have no idea how to search inside the bytearray
So searching the Bytes and the Position inside the Array is the biggest problem atm
-
Aug 12th, 2018, 06:20 PM
#5
Re: Find 3 Bytes in Byte Array
It may be a bit risky, but from the description it's pretty straightforward: Don't look for the three bytes, look for just the first byte. Once you find that, check the next two bytes to look for a match.
The risky point is that you may get false positives. If those three bytes happen to occur as part of something else, and are not what you are looking for, the replacement could create a mess.
With that caveat, the solution is just to loop through each byte in the array looking for one that is 20. That would just be:
Code:
For x = 0 to bytes.Count-3 'No point in checking the last two bytes.
If bytes(x) = &H20 Then
if bytes(x+1) = &HBB AndAlso bytres(x+2) = &HBD Then
'It was found at position X, so save X somewhere and keep on going.
End If
End If
Next
My usual boring signature: Nothing
-
Aug 13th, 2018, 01:04 PM
#6
Re: Find 3 Bytes in Byte Array
<edit> You can ignore my posts, I was in a VB6 frame of mind and posted VB6 code, not realizing I was responding to a VB.net question. </edit>
You can search a byte array to find a byte sequence using InStrB.
You just need a byte array containing the pattern you're looking for.
Code:
Private Sub Command1_Click()
Dim b() As Byte
b = "This is a test and we will see what will happen if I try to search the byte array for three bytes"
b(40) = &H20
b(41) = &HFF
b(42) = &HBD
Dim s(2) As Byte
s(0) = &H20
s(1) = &HFF
s(2) = &HBD
Dim p As Long
p = InStrB(b, s)
Debug.Print p
End Sub
So, we just fill a byte array with some data (in this case, it happens to be a unicode byte stream).
Then we change a few bytes to the pattern we're looking for to test the search.
We create the pattern of bytes we're looking for.
We do the search.
Since we stored that pattern at offset 40 in the byte array (the 41st byte), we should see 41 printed in the Immediate window.
Last edited by passel; Aug 15th, 2018 at 10:25 AM.
-
Aug 14th, 2018, 05:44 AM
#7
Thread Starter
Member
Re: Find 3 Bytes in Byte Array
Originally Posted by lastyle
I dont expect a one Hour Solution at all. I`ve been searching the net for small Code examples that would search for atleast 2 Bytes inside an Byte Array but i couldnt find one.
Current Code is actually , select a Folder where my Files are, then list them in a Listbox, I have a "small" analyzing Logic that reads the first 2 Bytes from each File as i Need this for a start Offset on another Computer System.
Concering the Byte search inside the Array i actually have absolutely Nothing since i have no idea how to search inside the bytearray
So searching the Bytes and the Position inside the Array is the biggest problem atm
Thanks a lot, you said it is risky, but does exactly what i was hoping for since i know 100% that the maximal amount Bytes of an instuction what i am searching for in the different Byte Arrys is exactly theese 3 Bytes.
Tested that with multiple Files and worked just Perferct
-
Aug 14th, 2018, 01:38 PM
#8
Re: Find 3 Bytes in Byte Array
<edit> You can ignore my posts, I was in a VB6 frame of mind and posted VB6 code, not realizing I was responding to a VB.net question. </edit>
So, which did you end up using, a loop through the bytes, or using InStrB to do the search.
InStrB may be faster, but it might not make that much difference.
Modified the InStrB example to overwrite the bytes in three different points in the array, and added a loop, to find and print the offset of each point found. You should see it print 40, 50, 55.
Code:
Private Sub Command1_Click()
Dim b() As Byte
b = "This is a test and we will see what will happen if I try to search the byte array for three bytes"
b(40) = &H20
b(41) = &HFF
b(42) = &HBD
b(50) = &H20
b(51) = &HFF
b(52) = &HBD
b(55) = &H20
b(56) = &HFF
b(57) = &HBD
Dim s(2) As Byte
s(0) = &H20
s(1) = &HFF
s(2) = &HBD
Dim p As Long
p = InStrB(b, s)
Do While p <> 0
Debug.Print p - 1
p = InStrB(p + 1, b, s)
Loop
End Sub
Last edited by passel; Aug 15th, 2018 at 10:25 AM.
-
Aug 14th, 2018, 06:43 PM
#9
Re: Find 3 Bytes in Byte Array
I was curious about that, too, but haven't had time to test the different performance. In general, I would think that the loop would be faster than InStrB, but not for any particular reason. Strings are slower, but a string is just an array of bytes (or can always be thought of that way), so InStrB is likely just doing byte comparisons. The question is how it is doing a comparison on three bytes. If it checks all three bytes for each byte in the array, then the loop should be faster, but AndAlso would tale care of that, so I really can't decide.
My usual boring signature: Nothing
-
Aug 15th, 2018, 10:22 AM
#10
Re: Find 3 Bytes in Byte Array
I think you can ignore my input.
I went to test it and realized I was in the wrong forum.
My posts are actually VB6 code, so I wouldn't have suggested the approach if I realized I was on the .Net side of the forum.
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
|