2

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

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

1 Answers1

5

You also need setfill('0').

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358