This is not as clean as we would like, but you can change the fill symbol to 0 to complete the task:
your_stream << std::setw(2) << std::hex << std::setfill('0') << x;
Note, however, that the character you set to fill is βsticky,β so after that it will remain β0β until you restore it to a place with something like your_stream << std::setfill(' '); .
Jerry Coffin
source share