Skip to main content
Added the #include for the boost example
Source Link
Alexis Wilke
  • 21.2k
  • 11
  • 111
  • 183

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<>lexical_cast<> works.

My favorite method is the boost lexical_cast<>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).

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<>

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).

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).

deleted 182 characters in body
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575

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.

This was the original answer I put before the question changed to include the line:

let's assume you don't have boost

My favorite method is the boost lexical_cast<>

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 unmarshaledun-marshaled from a stream (Take a look at the >> and << operators).

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.

This was the original answer I put before the question changed to include the line:

let's assume you don't have boost

My favorite method is the boost lexical_cast<>

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 unmarshaled from a stream (Take a look at the >> and << operators).

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<>

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).

deleted 69 characters in body
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575

Thanks for the downvotes. After changing the question on me!

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.

This was the original answer I put before the question changed to include the line:

let's assume you don't have boost

My favorite method is the boost lexical_cast<>

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 unmarshaled from a stream (Take a look at the >> and << operators).

Thanks for the downvotes. After changing the question on me!

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.

This was the original answer I put before the question changed to include the line:

let's assume you don't have boost

My favorite method is the boost lexical_cast<>

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 unmarshaled from a stream (Take a look at the >> and << operators).

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.

This was the original answer I put before the question changed to include the line:

let's assume you don't have boost

My favorite method is the boost lexical_cast<>

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 unmarshaled from a stream (Take a look at the >> and << operators).

added 224 characters in body
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575
Loading
added 287 characters in body
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575
Loading
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575
Loading