The little test program below prints out:
And SS Number IS =3039
I would like the number to print out with padded left zeros such that the total length is 8. So:
And SS Number IS =00003039 (notice the extra zeros left padded)
And I would like to know how to do this using manipulators and a stringstream as shown below. Thanks!
The test program:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
int i = 12345;
std::stringstream lTransport;
lTransport << "And SS Number IS =" << std::hex << i << '\n';
std::cout << lTransport.str();
}
Have you looked at the library's setfill and setw manipulators?
#include <iomanip>
...
lTransport << "And SS Number IS =" << std::hex << std::setw(8) ;
lTransport << std::setfill('0') << i << '\n';
The output I get is:
And SS Number IS =00003039
I would use:
cout << std::hex << std::setw(sizeof(i)*2) << std::setfill('0') << i << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With