Another way to print hex value in C++

You can simply use the std::hex base format flag! This also works with oct and dec.

#include <iostream>

int main() {
    std::cout << "Value of 10 in hex: " << std::hex << 10 << std::endl;
    std::cout << "Value of 10 in dec: " << std::dec << 10 << std::endl;
    std::cout << "Value of 10 in oct: " << std::oct << 10 << std::endl;

    getchar();
}

Output:

Value of 10 in hex: a

Value of 10 in dec: 10

Value of 10 in oct: 12

leave your comment