Anton
Anton

Reputation: 13

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.

Upvotes: 1

Views: 2572

Answers (2)

ForEveR
ForEveR

Reputation: 55887

Why boost::lexical_cast? Use std::stringstream

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

Upvotes: 6

Some programmer dude
Some programmer dude

Reputation: 409176

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

Upvotes: 2

Related Questions