繁体   English   中英

如何让系统在十六进制数字之前打印 0x 和在八进制数字之前打印 0?

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

我的老师说,对于作业,我们不允许在十六进制数字之前手动打印 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM