c++stringboostlexical-cast

boost::lexical_cast int to string padding with zeros


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.


Solution

  • Why boost::lexical_cast? Use std::stringstream

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