Custom stream manipulator for streaming integers in any base

Viewed 6361

I can make an std::ostream object output integer numbers in hex, for example

std::cout << std::hex << 0xabc; //prints `abc`, not the base-10 representation

Is there any manipulator that is universal for all bases? Something like

std::cout << std::base(4) << 20; //I want this to output 110

If there is one, then I have no further question. If there isn't one, then can I write one? Won't it require me to access private implementation details of std::ostream?

Note that I know I can write a function that takes a number and converts it to a string which is the representation of that number in any base. Or I can use one that already exists. I am asking about custom stream manipulators - are they possible?

5 Answers
Related