quantum231 quantum231 - 1 month ago 9
C++ Question

Why does C++ print small case alphabet when using std::hex and not capital?

The std::hex causes the data put into a stream to be printed as hexadecimal values. However, the letters A-F used to express number 10 to 15 always appear in small case alphabet. Is there a way to change it to use capital letters instead?

Answer

Is there a way to change it to use capital letters instead?

Yes, you can apply the std::uppercase I/O manipulator to change the default behavior (which is lowercase).

The example from the reference mentioned above:

#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';
}

Output:

0x2a with uppercase: 0X2A
0x2a with nouppercase: 0x2a
1e-10 with uppercase: 1E-10
1e-10 with nouppercase: 1e-10
Comments