如何使用 VB.NET 在字节数组中查找字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20727130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:14:08  来源:igfitidea点击:

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

arraysvb.netsearchfindbytearray

提问by Afnan Makhdoom

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.

例如,我使用Instr(String, "something to search")InstrRev(String, "something to search")用于字符串。我基本上不想遍历字节数组,因为我有非常长的字节数组,我想快速搜索字节。

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?

什么是更快更简单的搜索方式?文件的字节数组,还是使用 filestream 流式传输文件然后在其中搜索?

回答by Bj?rn-Roger Kringsj?

The System.Arrayclass exposes many useful methods when working with arrays.

System.Array的使用数组时类暴露许多有用的方法。

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

回答by Shabeer Shabeer

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).

它仍然是相同的代码(在 VB.NET 中)。