Question

Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?

#include <iostream>
#include <iomanip>
int main()
{
   int n = 16;
   std::cout << ???(5) << n << std::endl;
}

I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?

My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:

#include <iostream>
#include <iomanip>
int main()
{
    unsigned short n = 0xA7;
    std::cout << std::hex << ???(4) << n << std::endl;
}

and I am expecting to get this output: 00A7

Was it helpful?

Solution

You also need setfill('0').

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top