C 检查数据是否已指定字符串

C++ Checking if data has specified string

本文关键字:字符串 是否 检查 数据      更新时间:2023-10-16

,因此我试图检查字符串是否在内存的'块中。因此,这是我开始并想从。

开始检查的编织内存地址0x00343211

我要做的是将从0x003432110x00343211 + 900的数据写入一个char数组,然后检查该字符串是否正在寻找。

所以这就是我已经尝试过的

char dataBuf[1000] = { 0 };
memcpy((void*)dataBuf,(void*)0x00343211,900);
if(strstr(dataBuf,"ACTIVE") != NULL)
{
    //I want to check if the string "ACTIVE" is
    //within the random data that I have written into dataBuf
}

但这似乎不起作用。

您可以直接在内存块上使用std ::搜索,并祈祷您的编译器具有有效的实现,例如:

#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    char dataBuf[13] = { "xxxACTIVExxx" }; // block of 12 bytes + zero byte
    std::string active = "ACTIVE";
    using std::begin;
    using std::end;
    // std::search returns dataBuf+12 if no match is found
    if (std::search(dataBuf, dataBuf + 12,
        begin(active), end(active))
        != dataBuf + 12)
    {
        std::cout << "ACTIVE has been foundn";
    }
    else {
        std::cout << "ACTIVE has not been foundn";
    }

    return 0;
}