C++ cout hex values?

user34537 picture user34537 · Jan 26, 2009 · Viewed 241.3k times · Source

I want to do:

int a = 255; 
cout << a;

and have it show FF in the output, how would I do this?

Answer

Greg Hewgill picture Greg Hewgill · Jan 26, 2009

Use:

#include <iostream>

...

std::cout << std::hex << a;

There are many other options to control the exact formatting of the output number, such as leading zeros and upper/lower case.