You can use a stringstream
instead of cout
.
cout
is just one special ostream that is created by default and is hooked up to the program's standard output. You can create other ostream objects that write to different things. std::stringstream
writes to a std::string
inside it.
#include <sstream>
std::string to_hex() {
unsigned int x = 256;
std::stringstream s;
s << std::hex << x;
return s.str();
}