好的。所以我写了这段代码,它所做的只是打印16个十六进制值,然后打印16个字符,然后为下一行打印换行符。在for循环中,我注意到我必须以某种方式使它打印127以上的任何东西,为什么我不知道,这就是为什么我在这里问你。cout奇怪的行为与cout?
最重要的是,任何人都有使用与十六进制cout任何指针?因为我不得不导入一个额外的模块只是为了设置cout的宽度为2.
这段代码刚刚完成了练习,我想输出看起来像它在十六进制编辑器HxD中的样子。我想我可以只使用一个字符串,而不必在字符串声明中复制两次相同的值。
但是,是的,任何指针或批评,请不要退缩。
#include <iostream>
#include <fstream>
#include <string>
#define SIZE 0x830
using namespace std;
int main(int argc, char * argv[])
{
int i, j, k;
ifstream dump(argv[1], ios::binary);
char* buffer = new char[SIZE];
if (dump.is_open())
{
dump.read(buffer, SIZE);
dump.close();
}
else
{
cout << "no success :(\n";
exit(0);
}
std::string my_hex;
std::string my_char;
for (int i = 0; i < SIZE; i++)
{
my_hex += buffer[i];
my_char += buffer[i];
}
for (int j = 0; j <= 2095; j++) //j is the position we are in the loop, which means that j also equals current character.
{
if ((j % 16 == 0) && (j != 0)) //if j is divisible by 16 and does not equal 0 then enter this if block, otherwise just print my_hex[j]
{
for (k = 0; k <= 15; k++)
{ //handle regular characters
if ((my_char[(j + k) - 16] >= 0) && (my_char[(j + k) - 16] <= 31) || (my_char[(j + k)] == 255))
{ // this checks if the value is lower than 31 or equal to 255, either which can have a '.' printed in its place.
cout << ".";
}
else if ((my_char[(j + k) - 16] >= 32) && (my_char[(j + k) - 16] <= 127))
{ //if value is greater than or equal to 32 or equal to or less than 127 it will print that character, remember to print the correct character. we must go back to the beginning of the string with - 16.
cout << my_char[(j + k) - 16];
}
else
{
cout << my_char[(j + k) - 16]; //prints everything above 127
};
}
cout << "\n";
}
printf("%02x ", (unsigned char) my_hex[j]); //print hexadecimal values of characters
}
dump.close();
return 0;
}
如果你认为你知道更好的方法,那么请一定赐教:) *我曾在CMD交换代码页1252(ANSI)字符输出看的权利相比,HXD 。
这是'std :: cout << std :: hex << 19 << std :: endl;'你在找什么? –
使用'std :: hex'有什么问题? – user0042
你的意思是在forloop条件。而不是'for(i = 0; i> = 2095; i ++)'而不是'for(i = 0; i> = length.my_hex; i ++)''? – james28909