Question

How can I search within an array of byte for a specific byte?

For example, I use Instr(String, "something to search") or InstrRev(String, "something to search") for strings. I basically don't want to loop through the byte array, because I have extremely long byte arrays and I want to search bytes in a flash.

I just need the fastest and the simplest code possible for my task.

What would be a faster quicker and simpler way to search? A byte array of a file, or to stream a file with filestream and then to search within it?

Was it helpful?

Solution

The System.Array class exposes many useful methods when working with arrays.

Dim [array] As Byte() = New Byte() {1, 2, 4, 6, 3}
Dim find As Byte = 3
Dim index As Integer = System.Array.IndexOf(Of Byte)([array], find)

If (index = -1) Then
    'Not found...
Else
    'Found!
End If

OTHER TIPS

Use:

Dim Tag As Byte, cTag() As Byte = UTF8Encoding.UTF8.GetBytes("Host: ")  ' For poc
Dim Ret As Byte, cTagLength as Byte = cTag.Length 'POS

For Each Ret In cBuffer 'Ret pos seek "Host: "
    If cBuffer(Ret) = cTag(Tag) Then
        Tag = +1 ' Avryleter
    Else
        Tag = 0 ' Reset
    End If
    If Tag = cTagLength Then 
        Exit For
    End If
Next

It still the same code (in VB.NET).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top