C++ stoi() Function to Convert String to int [5 Examples]

Featured image for How to Convert String to int in-C+ Tutorial

What is stoi() function in C++?

The stoi function:

  • The stoi() is the abbreviation of “string to an integer”.
  • As the full name suggests, stoi() is used to convert string to int in C++
  • stoi() function was introduced in C++ 11

Why do we need stoi() function?

This is quite common in different languages to convert one data type to another. This is generally called type casting.

One of the examples can be converting user entered strings into numbers to ensure the operation goes smoothly and the program does end up in error.

An example of converting a string to int by stoi() function

In the first example, we have declared a string with a number. i.e. str = “425”;

Then we used stoi() function to convert this string into an int and assigned it to an integer variable and displayed its value:

#include <iostream>
#include <string>

using namespace std;

int main() {

    //Creating a string variable
    string str = "425";
    //Declaring int variable
    int x;

    // using stoi() function for string to int conversion
    x = stoi(str);

    cout << "Value after conversion = " <<x <<"\n\n";

    return 0;

}

Output:

CPP-string-to-int

Taking user input string example

For this program, we take the user input and store in the string variable.

#include <iostream>
#include <string>

using namespace std;

int main() {

    //Creating a string variable

    string str;

    cout << "Please enter a string as number? ";
    cin >> str;

    //Declaring int variable to be used to store converted string
    int x;

    // using stoi() function for string to int conversion
    x = stoi(str);

    cout << "Value after conversion = " <<x <<"\n\n";

    return 0;

}

Sample Output:

CPP-string-to-int-inpu

What as I entered the alphabet i.e. ABC as input?

Output:

CPP-string-int-char

So, the string you are trying to convert to int should be numbers enclosed in double quotes. Otherwise, the above error is generated.

Using stoi() to convert hex string to int example

The stoi() function can also be used for converting hex string to int.

You may convert strings in other formats like:
  • simple numbers (as seen in the above examples)
  • binary
  • octal
  • hex

stoi() function syntax is:

    int stoi (const string&  str, [size_t* idx], [int base]);

The example below shows using the stoi() function for converting a hex string to int:

#include <iostream>
#include <string>     // string, std::stoi

using namespace std;

int main ()

{

  string str_hex = "034591";

  int int_hex = stoi (str_hex,nullptr,16);

  cout <<"Hex String: " <<str_hex  <<"\n\n";
  cout << "After Hex String to int conversion: " << int_hex << '\n';

  return 0;

}

Output:

CPP-hex-string

A C++ program for converting binary string to int

Similarly, you may convert binary string to int by using stoi() function as shown in an example below:

#include <iostream>
#include <string>     // string, std::stoi

using namespace std;

int main ()

{
  string str_bin = "0101001110";
  int int_bin = stoi (str_bin,nullptr,2);

  cout <<"Binary String: " <<str_bin  <<"\n\n";

  cout << "After Binary String to int conversion: " << int_bin << '\n';

  return 0;

}

Output:

CPP-binary-string

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!