How to convert the std::string to multiple common types in C++

1 Answer

0 votes
#include <iostream>
#include <string>

/*
Conversion Quick Reference
--------------------------------------
std::stoi(str); // int
std::stol(str); // long
std::stoll(str); // long long
 
std::stoul(str); // unsigned long
std::stoull(str); // unsigned long long
 
std::stof(str); // float
std::stod(str); // double
std::stold(str); // long double
 
str.c_str(); // const char*
str.at(0); // char
*/

int main()
{
    // Source strings
    std::string intStr = "42";
    std::string longStr = "123456789";
    std::string doubleStr = "3.14159";
    std::string boolStr = "1";
    std::string charStr = "A";

    try
    {
        /*
         * Convert std::string to int
         */
        int intValue = std::stoi(intStr);

        /*
         * Convert std::string to long
         */
        long longValue = std::stol(longStr);

        /*
         * Convert std::string to float
         */
        float floatValue = std::stof(doubleStr);

        /*
         * Convert std::string to double
         */
        double doubleValue = std::stod(doubleStr);

        /*
         * Convert std::string to bool
         * Convention:
         * "0" = false
         * non-zero = true
         */
        bool boolValue = (std::stoi(boolStr) != 0);

        /*
         * Convert std::string to char
         */
        char charValue = charStr.at(0);

        /*
         * Convert std::string to C-style string
         */
        const char* cString = intStr.c_str();

        /*
         * Convert std::string to unsigned int
         */
        unsigned int uintValue =
            static_cast<unsigned int>(std::stoul(intStr));

        /*
         * Convert std::string to long long
         */
        long long llValue = std::stoll("922337203685477580");

        /*
         * Convert std::string to unsigned long long
         */
        unsigned long long ullValue =
            std::stoull("184467440737095516");

        std::cout << "int: " << intValue << '\n';
        std::cout << "long: " << longValue << '\n';
        std::cout << "float: " << floatValue << '\n';
        std::cout << "double: " << doubleValue << '\n';
        std::cout << "bool: " << std::boolalpha
                  << boolValue << '\n';
        std::cout << "char: " << charValue << '\n';
        std::cout << "c-string: " << cString << '\n';
        std::cout << "unsigned int: " << uintValue << '\n';
        std::cout << "long long: " << llValue << '\n';
        std::cout << "unsigned long long: "
                  << ullValue << '\n';
    }
    catch (const std::invalid_argument& e) {
        std::cerr << "Invalid conversion: "
                  << e.what() << '\n';
    }
    catch (const std::out_of_range& e) {
        std::cerr << "Value out of range: "
                  << e.what() << '\n';
    }
}



/*
run:

int: 42
long: 123456789
float: 3.14159
double: 3.14159
bool: true
char: A
c-string: 42
unsigned int: 42
long long: 922337203685477580
unsigned long long: 184467440737095516

*/

 



answered Oct 20 by avibootz
...