int main()
{
char B[76]={0};
ifstream infile;
infile.open("tworecords.dat", ios::binary);
infile.read(reinterpret_cast<char*>(B), sizeof (B));
cout << "Array B in hex" << endl;
for (int i = 0; i < 76; i++)
{
cout << hex << B[i] << " " << endl;;
}
return 0;
}
现在它正确读取数据,但将值打印为ASCII符号。我想在文件中输出实际的十六进制值。
示例:
01 3D 76 D6 等
答案 0 :(得分:4)
将其转换为整数:
cout << hex << static_cast<int>(B[i]) << " " << endl;
或者,如果你不想演员,只需加0:
cout << hex << B[i]+0 << " " << endl;
但是,您可能还希望确保对于低于16的值,打印前导0
(例如,换行符0A
,而不仅仅是A
):
cout << setfill('0') << setw(2) << hex << B[i]+0 << " " << endl;
答案 1 :(得分:0)
您只需将数字转换为整数:
cout << hex << (int)B[i] << " " << endl;
<iostream>
库(实际上是所有流库)输出char
类型的ascii值。