【发布时间】:2023-04-03 01:04:01
【问题描述】:
我想为我在参数中输入的二进制文件获取这个十六进制表示法:
我得到的输出和我想要的:
这是我写的代码,我没有好的十六进制数(对于 5A 之后的部分),我做错了什么?如何将我读取的字节正确转换为十六进制? 谢谢。
int main(int argc, char *argv[])
{
std::string parameter = "The\\Path\\To\My\exe.exe";
ifstream::pos_type size;
char * memblock;
ifstream file(parametre, ios::in | ios::binary | ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char[size];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
cout << "the complete file content is in memory" << endl;
string str = string(memblock, size);
string hexContent = "";
int maxColumn = 0;
std::stringstream ss;
int column = 0;
for (int i = 0; i < size; ++i)
{
ss << std::hex << (int)str[i];
if (column == 8)
{
ss << '\n';
column = 0;
}
column++;
}
std::string mystr = ss.str();
cout << mystr;
}
return 0;
}
【问题讨论】:
-
输入是什么,输出是什么?