【问题标题】:How do I make the system print 0x before the hex number & 0 before the octal number?如何让系统在十六进制数字之前打印 0x 和在八进制数字之前打印 0?
【发布时间】:2020-01-23 00:45:43
【问题描述】:

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

【问题讨论】:

    标签: c++ hex octal


    【解决方案1】:

    使用std::showbase:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多