如何在BYTE数组中搜索模式? [英] How to search in a BYTE array for a pattern?

查看:47
本文介绍了如何在BYTE数组中搜索模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组:

BYTE Buffer [20000]; 该数组包含以下数据:

BYTE Buffer[20000]; this array contains the following data:

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C12000205A1300

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C4C2050323231300A2020000000FD00384B1E5310000A20202020202000FA

我的问题是如何在此数组中搜索" 000000FC "之类的模式?我并不真正认为这很重要,但是我也需要索引来找到我的模式.有人可以为此提供一个示例,因为我不太了解:(

My question is how can i search this array for a pattern like "000000FC"? I don't really think it is important, but i need the index where i can find my pattern too. Could someone provide an example for this, because i don't really understand this :(

推荐答案

由于您使用的是C ++,请以C ++的方式进行操作:

Since you're in C++, do it the C++ way:

char a[] = { 0, 0, 0, 0xFC };
char Buffer[20000] = ...

std::string needle(a, a + 4);
std::string haystack(Buffer, Buffer + 20000);  // or "+ sizeof Buffer"

std::size_t n = haystack.find(needle);

if (n == std::string::npos)
{
    // not found
}
else
{
    // position is n
}

您还可以使用一种算法直接搜索数组:

You can also use an algorithm to search the array directly:

#include <algorithm>
#include <iterator>

auto it = std::search(
    std::begin(Buffer), std::end(Buffer),
    std::begin(a), std::end(a));

if (it == std::end(Buffer))
{
    // not found
}
else
{
    // subrange found at std::distance(std::begin(Buffer), it)
}

或者,在C ++ 17中,您可以使用字符串视图:

Or, in C++17, you can use a string view:

std::string_view sv(std::begin(Buffer), std::end(Buffer));

if (std::size_t n = sv.find(needle); n != sv.npos)
{
    // found at position n
}
else
{
    // not found
}

这篇关于如何在BYTE数组中搜索模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆