How to use cout manipulator to set hex output with uppercase in C++

1 Answer

0 votes
#include <iostream>

using namespace std;

ostream &sethex(ostream &stream)
{
	stream.unsetf(ios::dec | ios::oct);
	stream.setf(ios::hex | ios::uppercase | ios::showbase);

	return stream;
}

ostream &noset(ostream &stream)
{
	stream.unsetf(ios::hex | ios::uppercase | ios::showbase);
	stream.setf(ios::dec);

	return stream;
}

int main()
{
	cout << sethex << 65 << endl;
	cout << noset << 65 << endl;

	return 0;
}


/*
run:

0X41
65

*/

 



answered Apr 9, 2018 by avibootz

Related questions

1 answer 181 views
1 answer 212 views
1 answer 315 views
1 answer 187 views
1 answer 148 views
1 answer 152 views
...