Printing hex values using cout()

I wanted to display hex numbers and came across setf() & unsetf() functions which are quite useful to know of! There are different kinds of flags & masks you can use as well.

Please refer http://www.cplusplus.com/reference/iostream/ios_base/setf/ for more information.

#include <iostream>
using namespace std;

int main()
{
    int num = 255;
    cout.setf(ios::hex, ios::basefield);
    cout << "Hex: " << num << endl;

    cout.unsetf(ios::hex);
    cout << "Original format: " << num << endl;

    return 0;
}

leave your comment