试图输出 exe 文件中的所有内容
Trying to output everything inside an exe file
我正在尝试输出此 .exe 文件的明文内容。它包含明文内容,例如 "Changing the code in this way will not affect the quality of the resulting optimized code." 微软放入 .exe 文件的所有内容。当我 运行 以下代码时,我得到 M Z E
的输出,后跟一颗心和一颗钻石。我做错了什么?
ifstream file;
char inputCharacter;
file.open("test.exe", ios::binary);
while ((inputCharacter = file.get()) != EOF)
{
cout << inputCharacter << "\n";
}
file.close();
您已经以二进制形式打开了流,这对于预期目的是有用的。但是,您按原样打印每个二进制数据:其中一些字符不可打印,从而产生奇怪的输出。
可能的解决方案:
如果您想打印 exe 的内容,您将获得比可打印字符更多的不可打印字符。因此,一种方法可能是打印十六进制值:
while ( file.get(inputCharacter ) )
{
cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff) << "\n";
}
或者您可以使用显示十六进制值的调试器方法,然后显示可打印的字符或“.”。如果不是:
while (file.get(inputCharacter)) {
cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff)<<" ";
if (isprint(inputCharacter & 0xff))
cout << inputCharacter << "\n";
else cout << ".\n";
}
嗯,为了人体工程学,如果 exe 文件包含任何真正的 exe,你最好选择在每一行显示几个字符;-)
二进制文件是字节的集合。字节的取值范围为 0..255。可以安全 "printed" 的可打印字符形成一个更窄的范围。假设最基本的 ASCII 编码
- 32..63
- 64..95
- 96..126
- 另外,如果您的代码页有它们,也许还有一些高于 128 的值
参见 ascii table。
超出该范围的每个字符至少可以:
- 打印出来不可见
- 打印成一些奇怪的垃圾
- 实际上是一个控制字符,将更改您的终端设置
一些终端支持 "end of text" 字符,之后将停止打印任何文本。也许你中了那个。
我会说,如果您只对文本感兴趣,那么只打印可打印的内容而忽略其他内容。或者,如果您想要所有内容,那么也许可以将它们写成十六进制形式?
我会使用 std::isprint 之类的东西来确保字符是可打印的,而不是在打印之前使用一些奇怪的控制代码。
像这样:
#include <cctype>
#include <fstream>
#include <iostream>
int main()
{
std::ifstream file("test.exe", std::ios::binary);
char c;
while(file.get(c)) // don't loop on EOF
{
if(std::isprint(c)) // check if is printable
std::cout << c;
}
}
这有效:
ifstream file;
char inputCharacter;
string Result;
file.open("test.exe", ios::binary);
while (file.get(inputCharacter))
{
if ((inputCharacter > 31) && (inputCharacter < 127))
Result += inputCharacter;
}
cout << Result << endl;
cout << "These are the ascii characters in the exe file" << endl;
file.close();
我正在尝试输出此 .exe 文件的明文内容。它包含明文内容,例如 "Changing the code in this way will not affect the quality of the resulting optimized code." 微软放入 .exe 文件的所有内容。当我 运行 以下代码时,我得到 M Z E
的输出,后跟一颗心和一颗钻石。我做错了什么?
ifstream file;
char inputCharacter;
file.open("test.exe", ios::binary);
while ((inputCharacter = file.get()) != EOF)
{
cout << inputCharacter << "\n";
}
file.close();
您已经以二进制形式打开了流,这对于预期目的是有用的。但是,您按原样打印每个二进制数据:其中一些字符不可打印,从而产生奇怪的输出。
可能的解决方案:
如果您想打印 exe 的内容,您将获得比可打印字符更多的不可打印字符。因此,一种方法可能是打印十六进制值:
while ( file.get(inputCharacter ) )
{
cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff) << "\n";
}
或者您可以使用显示十六进制值的调试器方法,然后显示可打印的字符或“.”。如果不是:
while (file.get(inputCharacter)) {
cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff)<<" ";
if (isprint(inputCharacter & 0xff))
cout << inputCharacter << "\n";
else cout << ".\n";
}
嗯,为了人体工程学,如果 exe 文件包含任何真正的 exe,你最好选择在每一行显示几个字符;-)
二进制文件是字节的集合。字节的取值范围为 0..255。可以安全 "printed" 的可打印字符形成一个更窄的范围。假设最基本的 ASCII 编码
- 32..63
- 64..95
- 96..126
- 另外,如果您的代码页有它们,也许还有一些高于 128 的值
参见 ascii table。
超出该范围的每个字符至少可以:
- 打印出来不可见
- 打印成一些奇怪的垃圾
- 实际上是一个控制字符,将更改您的终端设置
一些终端支持 "end of text" 字符,之后将停止打印任何文本。也许你中了那个。
我会说,如果您只对文本感兴趣,那么只打印可打印的内容而忽略其他内容。或者,如果您想要所有内容,那么也许可以将它们写成十六进制形式?
我会使用 std::isprint 之类的东西来确保字符是可打印的,而不是在打印之前使用一些奇怪的控制代码。
像这样:
#include <cctype>
#include <fstream>
#include <iostream>
int main()
{
std::ifstream file("test.exe", std::ios::binary);
char c;
while(file.get(c)) // don't loop on EOF
{
if(std::isprint(c)) // check if is printable
std::cout << c;
}
}
这有效:
ifstream file;
char inputCharacter;
string Result;
file.open("test.exe", ios::binary);
while (file.get(inputCharacter))
{
if ((inputCharacter > 31) && (inputCharacter < 127))
Result += inputCharacter;
}
cout << Result << endl;
cout << "These are the ascii characters in the exe file" << endl;
file.close();