为什么 C++ 在使用 std::hex 而不是大写字母时打印小写字母表?

qua*_*231 3 c++ hex

std::hex 导致放入流中的数据被打印为十六进制值。但是,用于表示数字 10 到 15 的字母 AF 总是以小写字母出现。有没有办法将其更改为使用大写字母?

πάν*_*ῥεῖ 5

有没有办法将其更改为使用大写字母?

是的,您可以应用std::uppercaseI/O 操纵器来更改默认行为(小写)。

上面提到的参考文献中的例子:

#include <iostream>
int main()
{
    std::cout << std::hex << std::showbase
              << "0x2a with uppercase: " << std::uppercase << 0x2a << '\n'
              << "0x2a with nouppercase: " << std::nouppercase << 0x2a << '\n'
              << "1e-10 with uppercase: " << std::uppercase << 1e-10 << '\n'
              << "1e-10 with nouppercase: " << std::nouppercase << 1e-10 << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:

0x2a with uppercase: 0X2A
0x2a with nouppercase: 0x2a
1e-10 with uppercase: 1E-10
1e-10 with nouppercase: 1e-10
Run Code Online (Sandbox Code Playgroud)