I want to overload
ostream& operator<<(ostream& out, const myType& y)
in order to output big unsigned integers represented in myType by a vector of unsigned short. So if the vector in question has, say, the elements 1f, a356, 13d5 I want to get the output 1fa35613d5 — right now I only need this for hex or oct output.
In particular, 1, 0, 0 should be output to 100000000. I want to acchieve this by successively outputting the elements of the vector. What I do get with this method, however, is 100, despite the fact that I set
out.width(4);
out.fill('0');
out << std::internal;
out << std::noskipws;
Of course I could first write the ushort to a string and then output that, but I’d prefer to only use formatting instructions for out cause this makes it easier to respect the hex or oct settings of out. Which formatting option am I missing here?
The following program prints fixed-width hex characters:
Output:
If you’re writing a formatting function for this, you don’t have to repeat
std::hex, as that’s permanent. Preserving the state of an ostream is a bit tricky, though, so perhaps you should look into something like Boost’s state-saver.