如何使系统在十六进制数之前打印 0x,在八进制数之前打印 0?

How do I make the system print 0x before the hex number & 0 before the octal number?

本文关键字:打印 八进制数 0x 系统 何使 十六进制数      更新时间:2023-10-16

我的老师说,对于作业,我们不允许在十六进制数字之前手动打印0x,我们必须让系统这样做。

目前我的代码如下所示:

cout << "Hex" << setw(12) << hex << static_cast<int>(letter) << setw(12) << hex
<< number << setw(12) << hex << static_cast<int> (symbol) << endl;

它打印正确的十六进制值,但没有 0x。

此外,对于八进制数字,我必须再次使系统在数字之前打印 0(不是手动。我的代码打印正确的值,但没有前面的 0:

cout << "Octal" << setw(12) << setbase(8) << static_cast<int>(letter) << setw(12) << setbase(8)
<< number << setw(12) << setbase(8) << static_cast<int>(symbol) << endl;

使用std::showbase

std::cout << std::hex << std::showbase << 1234567890;