Possible duplicate:
Print leading zeros with C ++ output operator (equivalent to printf)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the result to be 00016
setw()adding spaces. Isn't it customizable which characters should be added with setw()?
My ultimate goal is to print a hexadecimal 2-byte number in 4 positions. Something like that:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I expect to get this result: 00A7
source
share