Afnan Makhdoom Afnan Makhdoom - 1 year ago 267 Question

How to find byte in array of bytes using VB.NET

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

For example, I use

Instr(String, "something to search")
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?

Answer Source

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...
End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download