如何在表示二进制文件的字符串中查找整数

时间:2016-11-02 11:12:13

标签: c++

如何在代表二进制文件的字符串中找到一个整数?

例如,

outStream.write(<data_buffer>); outStream.flush();

我需要知道const std::string pe_file_path(argv[1]); std::ifstream pe_file(pe_file_path, std::ios_base::binary); const std::string pe_file_content((std::istreambuf_iterator<char>(pe_file)), std::istreambuf_iterator<char>()); DWORD some_value = 0x243e0c10; // pe_file_content.find(???); 在字符串中的位置。

我该怎么做?

现在我正在使用以下解决方案

some_value

但我想这个问题可以有更优雅的解决方案。

2 个答案:

答案 0 :(得分:2)

如果pe_file_content包含四个字节0x240x3E0x0C0x10,而不是八个字节代表字符,那么你需要将DWORD值转换为包含相同字节的字符串,然后您可以搜索它:

std::string needle(reinterpret_cast<const char*>(&some_value), sizeof(some_value));
pe_file_content.find(needle);

小心字节顺序。如果文件中的整数与机器的整数不同,则在搜索之前需要反转some_value的字节顺序。

答案 1 :(得分:0)

我猜你可以使用相同的方法here将DWORD转换为字符串流,并使用string :: find函数来查找数字的位置。