Use the C++ streams.
std::string plop("123"); std::stringstream str(plop); int x; str >> x; /* Lets not forget to error checking */ if (!str) { // The conversion failed. // Need to do something here. // Maybe throw an exception }
PS. This basic principle is how the boost library lexical_cast<> works.
My favorite method is the boost lexical_cast<>
#include <boost/lexical_cast.hpp> int x = boost::lexical_cast<int>("123");
It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).