【问题标题】:How to find byte in array of bytes using VB.NET如何使用 VB.NET 在字节数组中查找字节
【发布时间】:2014-01-10 16:52:51
【问题描述】:

如何在字节数组中搜索特定字节?

例如,我将Instr(String, "something to search") 或InstrRev(String, "something to search") 用于字符串。我基本上不想循环遍历字节数组,因为我有非常长的字节数组,我想在闪存中搜索字节。

我只需要最快和最简单的代码来完成我的任务。

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

【问题讨论】:

    标签: arrays vb.net search find bytearray


    【解决方案1】:

    用途:

    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
    

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

    【讨论】:

    • 这是一个新问题还是您试图回答原来的问题?
    【解决方案2】:

    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
    

    【讨论】:

    • 非常感谢朋友 :)
    • 你能告诉我如何找到2个或更多连续字节模式的索引吗?同样的方式?
    • @AfnanMakhdoom 看看这个帖子,byte() array pattern search。转换developerfusion处的代码。
    • 如何在另一个数组中搜索一个数组的多个点,例如 array1(2) + array1(3) + array1(4) 在另一个数组中说 array2?
    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2012-07-07
    相关资源
    最近更新 更多