boost::lexical_cast int to string padding with zeros

2.4k views Asked by At

I need to create files with generated names. I use boost::lexical_cast to transform integers to std::string. Is it a possibility to get string with padding zeros; I have no c++11 tools, just everything that MSVS 2008 supports.

Example :

int i = 10;
std::string str = boost::lexical_cast<std::string>(i);

// str = "10"
// expect str = "000010"

p.s. don't suggest to use sprintf please.

2

There are 2 answers

1
ForEveR On BEST ANSWER

Why boost::lexical_cast? Use std::stringstream

std::ostringstream ss;
ss << std::setw(6) << std::setfill('0') << i;
const std::string str = ss.str();
0
Some programmer dude On

You could use std::ostringstream with the normal stream manipulators for formatting.