C++ is one of the most popular and oldest programming languages used for the development of high-performance applications. It is used among highly expert programmers and novice developers. It is mainly used in game development and is an ideal option for developing operating systems. When writing code in the C++ language, you may come across converting data from one type to another. In this article, you will learn to convert a string into a long integer. It is quite difficult to convert a string into any other format. However, the C++ programming language provides a quick and efficient way to convert strings into integers.
stol Function in C++
The stol is a C++ built-in function provided in the std library. It converts strings to long integer numbers. It converts the string content into an integer number of the specified base and returns a long integer value. It takes three parameters, one is the input string, the second is the object parameter, and the third one is the numerical base. The syntax of the stol function is given below, have a look:
The function stol takes three parameters: input_string, size, and base. The “input_string” represents the input string that needs to be converted into a long integer number.
The “size” parameter represents the object of type size_t which carries the next character position in the input string after the numerical value. It can be a null pointer if it is not required to use.
The third parameter, the “base” parameter represents the base in which the string needs to be interpreted. For example, if the “2” base is given, the string will be interpreted as the integer base 2 number. The default value of the base parameter is 10 and if 0 is provided then the base value will be determined by the format in the sequence. The stol function returns the long integer number representing the int value of a given string.
Now, let us explore some examples to understand how the stol function works in C++.
Example # 1
In this example, we will provide decimal and hexadecimal strings to check how the stol function converts them into decimal and hexadecimal integer numbers.
The “dec = 123456789” is a decimal string and converted into a long integer using the stol function stol(dec,&size). Note that the base is not provided in the function as an input parameter which as result uses the default base 10 to convert the string into a decimal number. However, for converting the string from a hexadecimal number to a decimal number, base 16 is provided as the input parameter stol(hex, nullptr, 16). As we know, the 16 base represents a hexadecimal number.
#include <string>
#include <stdlib.h>
int main ()
{
std::string dec = "123456789";
std::string hex = "a2bf3c";
std::string::size_type size;
long lidec = std::stol (dec,&size);
long lihex = std::stol (hex,nullptr,16);
std::cout << "Input decimal string "<<dec << " converted to long int " << lidec << '\n';
std::cout << "Input hexadecimal string "<<hex << " converted to long int " << lihex << '\n';
return 0;
}
Here is the following output. Note that the decimal string “123456789” converted to “123456789” decimal long integer. While the hexadecimal string “a2bf3c” converted to “10665788” hexadecimal number. Here are the required steps that you can follow to learn how the conversion is done by the stol function:
Example # 2
In this example, we will convert a binary number using the stol function. Let’s see the code below and understand the working of the code. The “1010110110” is given as the input string and base 2 is provided as the input base parameter stol(bin,&size, 2), representing the number in binary format. The stol function will convert the binary number into a decimal number by following these steps:
#include <string>
#include <stdlib.h>
int main ()
{
std::string bin = "1010110110";
std::string::size_type size;
long libin = std::stol (bin,&size,2);
std::cout << "Input binary string "<<bin << " converted to long int " << libin << '\n';
return 0;
}
As you can see that the stol function returned the same result as the general binary to decimal conversion process did.
Example # 3
In this example, we are going to see what happens if we test the stol function with invalid input. A string of multiple characters will be provided to the stol function and 0 will be provided as the base value so that the function will determine the base of the string automatically. Here is the code:
A set of characters is provided as an input string which is not determined by any base values, i.e., 10, 16, 2, etc. so the function will return an error value.
#include <string>
#include <stdlib.h>
int main ()
{
std::string chr = "abcdefgh";
std::string::size_type size;
long str = std::stol (chr,nullptr,0);
std::cout << "Input character string "<<chr << " converted to long int " << str << '\n';
return 0;
}
See the output below. Note that the compiler has raised an “invalid_argument” exception since the function does not cater to strings that cannot be determined with any base value.
Example # 4
In this example, we will provide a combination of valid and invalid input to see the result of the stol() function.
The input string is a combination of valid and invalid characters, “123xf25”. The “0” isg provided as an input base so that the function determines the base of the input string automatically based on the type of characters.
#include <string>
#include <stdlib.h>
int main ()
{
std::string chr = "123xf25";
std::string::size_type size;
long str = std::stol (chr,nullptr,0);
std::cout << "Input string "<<chr << " converted to long int " << str << '\n';
return 0;
}
Here is the output that shows the working of stol function with a combination of valid and invalid input:
Note that the function converted the “123” into decimal number “123” and discarded the rest of the string as it received an invalid input “x”. The string after the character “x” is not converted by the stol function, returning only the first characters of the string as long int.
Conclusion
In this post, we explored the stol function of the C++ programming language. With the help of some useful and simple examples, we learned how the stol function works with different types of inputs. The stol function takes three parameters, the input string that is to be converted, a size parameter that represents the position of the function in the string, and the base value that represents the base of the input string. It returns the long int value of the input string.