Extension number from 0

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

+3
source share
1 answer

You will also need setfill('0').

+5
source

Source: https://habr.com/ru/post/1773477/


All Articles