C++ String stoull

The std::stoull function in C++ converts a string representation of an unsigned integral number into an unsigned long long. It is part of the C++11 standard and is defined in the <string> header. This function is useful for parsing strings that represent large numerical values, especially when dealing with user input or data read from files.


Syntax of string stoull function

unsigned long long stoull(const std::string& str, std::size_t* idx = 0, int base = 10);

Parameters of string stoull function

ParameterDescription
strThe 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 stoull function

On successful conversion, std::stoull returns the converted value as an unsigned long long. If the conversion fails, it throws an exception.


Exceptions for string stoull function

The std::stoull 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 long long type.

Examples for string stoull function

Example 1: Converting a Decimal String to Unsigned Long Long

In this example, we convert a string containing a decimal number to an unsigned long long using std::stoull.

Program

#include <iostream>
#include <string>

int main() {
    std::string str = "18446744073709551615"; // Maximum value for unsigned long long
    unsigned long long num = std::stoull(str);
    std::cout << "The unsigned long long value is: " << num << std::endl;
    return 0;
}

Output

The unsigned long long value is: 18446744073709551615

Explanation

  1. The program includes the necessary headers: <iostream> for input and output operations, and <string> for string handling.
  2. A string str is initialized with the value "18446744073709551615", which is the maximum value for an unsigned long long.
  3. The std::stoull function converts the string to an unsigned long long and stores it in the variable num.
  4. The program outputs the unsigned long long value using std::cout.

Example 2: Converting a Hexadecimal String to Unsigned Long Long

In this example, we convert a string representing a hexadecimal number to an unsigned long long by specifying the base as 16.

Program

#include <iostream>
#include <string>

int main() {
    std::string str = "0xFFFFFFFFFFFFFFFF"; // Maximum value for unsigned long long in hexadecimal
    unsigned long long num = std::stoull(str, nullptr, 16);
    std::cout << "The unsigned long long value is: " << num << std::endl;
    return 0;
}

Output

The unsigned long long value is: 18446744073709551615

Explanation

  1. The string str contains the hexadecimal representation of the maximum unsigned long long value.
  2. The std::stoull function is called with the base parameter set to 16, converting the hexadecimal string to a decimal unsigned long long.
  3. The resulting unsigned long long value is stored in num and printed using std::cout.

Examples for Exception Handling in stoull

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 long num = std::stoull(invalidStr);
    } catch (const std::invalid_argument& e) {
        std::cout << "Invalid argument exception: " << e.what() << std::endl;
    }
    return 0;
}

Output

Invalid argument exception: stoull

Explanation

  1. The string invalidStr contains non-numeric characters, making it an invalid input.
  2. When std::stoull is called, it attempts to convert the string to an unsigned long long, but since the string does not represent a valid number, the function throws a std::invalid_argument exception.
  3. The exception is caught in the catch block, and an appropriate error message is displayed using std::cout.