热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在字节数组中搜索模式?-HowtosearchinaBYTEarrayforapattern?

IhaveaBytearray:我有一个字节数组:BYTEBuffer[20000];thisarraycontainsthefollowingdata:字节缓冲区[

I have a Byte array :

我有一个字节数组:

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

字节缓冲区[20000];该数组包含以下数据:

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C4C2050323231300A2020000000FD00384B1E5310000A20202020202000FA

00 ffffffffffff0010ac4c4053433442341401030a2f1e78eeee95a3544c99260f5054a54b00714f8180b3000101010101010101010121399030621a274068b03600da281100001c000000ff003457314d44304353423443530a000000fc0044454c4c2050323231300a2020000000fd00384b1e5310000a20202020202000fa

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 :(

我的问题是,如何搜索这个数组以获得“000000FC”这样的模式?我不认为这很重要,但我需要索引,我也可以找到我的模式。有人能举个例子吗,因为我不太明白

3 个解决方案

#1


24  

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

既然你用的是c++,那就用c++吧:

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 
#include 

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

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

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

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
}

#2


6  

You want something like memmem (that code is licensed with the GPL).

您需要类似memmem的东西(该代码是由GPL授权的)。

However, it should not be difficult to roll your own. Like in memmem's implementation, you need a loop that uses memchr to find the first character of your needle in the haystack, and memcmp to test each hit and see if all of your needle is there.

然而,卷你自己的卷并不难。就像在memmem的实现中一样,您需要一个循环,使用memchr在haystack中查找针头的第一个字符,并使用memcmp测试每次命中,看看您的针头是否都在那里。

#3


2  

Try this, just needed it:

试试这个,只是需要它:

// Returns a pointer to the first byte of needle inside haystack, 
static uint8_t* bytes_find(uint8_t* haystack, size_t haystackLen, uint8_t* needle, size_t needleLen) {
    if (needleLen > haystackLen) {
        return false;
    }
    uint8_t* match = memchr(haystack, needle[0], haystackLen);
    if (match != NULL) {
        size_t remaining = haystackLen - ((uint8_t*)match - haystack);
        if (needleLen <= remaining) {
            if (memcmp(match, needle, needleLen) == 0) {
                return match;
            }
        }
    }
    return NULL;
}

推荐阅读
author-avatar
葛妹秀
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有