C++ String stoul
The std::stoul function in C++ converts a string representation of an unsigned integral number into an unsigned long. It is part of the C++11 standard and is defined in the <string> header.
Syntax of string stoul function
unsigned long stoul(const std::string& str, std::size_t* idx = 0, int base = 10);
Parameters of string stoul function
| Parameter | Description |
|---|---|
str | The string containing the representation of an unsigned integral number. |
idx (optional) | A pointer to a std::size_t object. If provided, it is set to the position of the first character in str after the numerical value. Defaults to 0. |
base (optional) | The numerical base (radix) for the conversion, which can range from 2 to 36. If set to 0, the base is automatically deduced from the string format (e.g., prefixes like 0x indicate base 16). Defaults to 10. |
Return Value of string stoul function
On successful conversion, std::stoul returns the converted value as an unsigned long. If the conversion fails, it throws an exception.
Exceptions for string stoul function
The std::stoul function can throw the following exceptions:
- std::invalid_argument: Thrown if the input string does not contain a valid unsigned integral number.
- std::out_of_range: Thrown if the converted value exceeds the range of the
unsigned longtype.
Examples for string stoul function
Example 1: Converting a Decimal String to Unsigned Long
In this example, we convert a string containing a decimal number to an unsigned long using std::stoul.
Program
#include <iostream>
#include <string>
int main() {
std::string str = "4294967295";
unsigned long num = std::stoul(str);
std::cout << "The unsigned long value is: " << num << std::endl;
return 0;
}
Output
The unsigned long value is: 4294967295
Explanation
- The program includes the necessary headers:
<iostream>for input and output operations, and<string>for string handling. - A string
stris initialized with the value"4294967295", which is the maximum value for a 32-bit unsigned integer. - The
std::stoulfunction converts the string to anunsigned longand stores it in the variablenum. - The program outputs the unsigned long value using
std::cout.
Example 2: Converting a Hexadecimal String to Unsigned Long
In this example, we convert a string representing a hexadecimal number to an unsigned long by specifying the base as 16.
Program
#include <iostream>
#include <string>
int main() {
std::string str = "0x1A3F"; // Hexadecimal value
unsigned long num = std::stoul(str, nullptr, 16); // Base 16 indicates hexadecimal
std::cout << "The unsigned long value is: " << num << std::endl;
return 0;
}
Output
The unsigned long value is: 6719
Explanation
- The program includes the necessary headers:
<iostream>for input/output operations and<string>for string handling. - The string
stris initialized with the hexadecimal value"0x1A3F". - The
std::stoulfunction is called with the base parameter set to 16, converting the hexadecimal string into its decimal equivalent. - The resulting unsigned long value is stored in
numand printed usingstd::cout.
Examples for Exception Handling in stoul
The std::stoul function can throw exceptions when the string cannot be converted to an unsigned long. Here are examples to demonstrate how to handle these exceptions:
Example 1: Handling std::invalid_argument
This exception is thrown when the string does not contain a valid numeric representation.
Program
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::string invalidStr = "HelloWorld";
unsigned long num = std::stoul(invalidStr);
} catch (const std::invalid_argument& e) {
std::cout << "Invalid argument exception: " << e.what() << std::endl;
}
return 0;
}
Output
Invalid argument exception: stoul
Explanation
- The string
invalidStrcontains characters that do not represent a valid number. - When
std::stoulis called, it detects the invalid input and throws astd::invalid_argumentexception. - The exception is caught in the
catchblock, and an error message is displayed usingstd::cout.
Example 2: Handling std::out_of_range
This exception is thrown when the value represented by the string exceeds the range of the unsigned long type.
Program
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::string largeStr = "99999999999999999999";
unsigned long num = std::stoul(largeStr);
} catch (const std::out_of_range& e) {
std::cout << "Out of range exception: " << e.what() << std::endl;
}
return 0;
}
Output
Out of range exception: stoul
Explanation
- The string
largeStrcontains a numeric value that exceeds the maximum range of theunsigned longtype. - When
std::stoulattempts to convert the string, it detects the out-of-range value and throws astd::out_of_rangeexception. - The exception is caught in the
catchblock, and an error message is displayed usingstd::cout.
