Created
April 28, 2015 07:26
-
-
Save mihids/a5314ee344da4357d11b to your computer and use it in GitHub Desktop.
Converting data types to adn from strings in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/lexical_cast.hpp> | |
inline std::string IntToString(unsigned int _uiIntToConvert) { | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::IntToString", "Int to convert: [" << _uiIntToConvert << "]"); | |
std::string sRet; | |
try { | |
sRet = boost::lexical_cast< std::string >(_uiIntToConvert); | |
} catch (boost::bad_lexical_cast&) { | |
sRet = ""; | |
} | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::IntToString", "Converted value: [" << sRet << "]"); | |
return sRet; | |
} | |
inline int StringToInt(std::string _sInt) { | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::StringToInt", "String to convert: [" << _sInt << "]"); | |
int iInt = -1; | |
try { | |
iInt = boost::lexical_cast< int >(_sInt); | |
} catch (boost::bad_lexical_cast&) { | |
iInt = -1; | |
} | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::StringToInt", "Converted int: [" << iInt << "]"); | |
return iInt; | |
} | |
inline double StringToDouble(std::string _sDouble) { | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::StringToDouble", "String to convert: [" << _sDouble << "]"); | |
double dDouble = -1; | |
try { | |
dDouble = boost::lexical_cast< double >(_sDouble); | |
} catch (boost::bad_lexical_cast&) { | |
dDouble = -1; | |
} | |
MODT_LOG_DEBUG(octopus::getRootLogger(), "EODUtils::StringToDouble", "Converted double: [" << dDouble << "]"); | |
return dDouble; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int Number = 123; // number to convert | |
char Result[16]; // string which will contain the number | |
sprintf ( Result, "%d", Number ); // %d makes the result be a decimal integer | |
std::cout << Result << std::endl; | |
char Text[] = "456"; // string to be converted | |
int ResultNum; // number which will contain the result | |
sscanf ( Text, "%d", &ResultNum ); | |
std::cout << ResultNum << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
int main() | |
{ | |
int Number = 123; // number to be converted to a string | |
string Result; // string which will contain the result | |
ostringstream convert; // stream used for the conversion | |
convert << Number; // insert the textual representation of 'Number' in the characters in the stream | |
Result = convert.str(); // set 'Result' to the contents of the stream | |
std::cout << "Int converted to String: " << Result << std::endl; | |
string Text = "456"; | |
if ( ! (istringstream(Text) >> Number) ) Number = 0; | |
std::cout << "String converted to Int: " << Number << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atoi( str.c_str() ); | |
std::stoi( str ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment