That's an old thread but here's my take on the topic
inline std::string hex(unsigned char c) { char h[]{"0xFF"}; sprintf(h, "0x%02X", c); return h; } std::cout << hex('\r')