所以我试图检查字符串是否在内存的“块”内。所以这里是一个组成的内存地址0x00343211
,我开始并希望从中开始检查。
我要做的是将数据从0x00343211
写入0x00343211 + 900
到char数组中,然后检查该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
}
但这似乎没有用。
答案 0 :(得分:0)
您可以直接在内存块上使用std::search,并祈祷您的编译器具有高效的实现,如下所示:
#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 found\n";
}
else {
std::cout << "ACTIVE has not been found\n";
}
return 0;
}