#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "0x" << setfill('0') << setw(2) << right << hex << 10 << endl;
}
If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers, yet it is type-safe.
If you want to make an easier way to output a hex number, you could write a function like this:
Updated version is presented below; there are two ways the 0x base indicator can be inserted, with footnotes detailing the differences between them. The original version is preserved at the bottom of the answer, so as not to inconvenience anyone that was using it.
Note that both the updated and original versions may need some tailoring for systems where the byte size is a multiple of 9 bits.
#include <type_traits> // For integral_constant, is_same.
#include <string> // For string.
#include <sstream> // For stringstream.
#include <ios> // For hex, internal, [optional] showbase.
// Note: <ios> is unnecessary if <iostream> is also included.
#include <iomanip> // For setfill, setw.
#include <climits> // For CHAR_BIT.
namespace detail {
constexpr int HEX_DIGIT_BITS = 4;
//constexpr int HEX_BASE_CHARS = 2; // Optional. See footnote #2.
// Replaced CharCheck with a much simpler trait.
template<typename T> struct is_char
: std::integral_constant<bool,
std::is_same<T, char>::value ||
std::is_same<T, signed char>::value ||
std::is_same<T, unsigned char>::value> {};
}
template<typename T>
std::string hex_out_s(T val) {
using namespace detail;
std::stringstream sformatter;
sformatter << std::hex
<< std::internal
<< "0x" // See footnote #1.
<< std::setfill('0')
<< std::setw(sizeof(T) * CHAR_BIT / HEX_DIGIT_BITS) // See footnote #2.
<< (is_char<T>::value ? static_cast<int>(val) : val);
return sformatter.str();
}
See both options (as detailed in footnotes, below) live: here.
Footnotes:
This line is responsible for showing the base, and can be either of the following:
<< "0x"
<< std::showbase
The first option will display improperly for custom types that try to output negative hex numbers as -0x## instead of as <complement of 0x##>, with the sign displaying after the base (as 0x-##) instead of before it. This is very rarely an issue, so I personally prefer this option.
If this is an issue, then when using these types, you can check for negativity before outputting the base, then using abs() (or a custom abs() that returns an unsigned value, if you need to be able to handle the most-negative values on a 2's complement system) on val.
The second option will omit the base when val == 0, displaying (e.g., for int, where int is 32 bits) 0000000000 instead of the expected 0x00000000. This is due to the showbase flag being treated like printf()'s # modifier internally.
If this is an issue, you can check whether val == 0, and apply special handling when it does.
Depending on which option was chosen for showing the base, two lines will need to be changed.
If using << "0x", then HEX_BASE_CHARS is unnecessary, and can be omitted.
If using << std::showbase, then the value supplied to setw() needs to account for this:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
/*This should print out 0x0a. The internal adjustment pads the width with the fill character*/
cout << hex << showbase << internal << setfill('0') << setw(4) << 10 << endl;
}
In C++20 you'll be able to use std::format to do this:
std::cout << std::format("{:02x}\n", 10);
Output:
0a
In the meantime you can use the {fmt} library, std::format is based on. {fmt} also provides the print function that makes this even easier and more efficient (godbolt):
fmt::print("{:02x}\n", 10);
Disclaimer: I'm the author of {fmt} and C++20 std::format.