#include <iostream> #include <iomanip> void show_a_byte(unsigned char val) { std::ostream out(std::cerr.rdbuf()); out << std::hex << std::setw(2) << std::setprecision(2) << static_cast<unsigned int>(val); }
I used the ostream cerr temporary clipboard to make sure that none of the manipulators left any unwanted side effects on cerr . static_cast necessary because when ostream receives ( signed or unsigned or plain) char , it believes that it can just copy it as the source byte.
source share