
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::stoull() function
The C++ std::string::stoull() function is used to convert a string to an unsigned long long integer. It analyse the string to extract the numerical value and ignores any whitespaces. It also supports optional parameters to specify the base for conversion, such as decimal, hexadecimal, or octal. If the conversion fails due to invalid input, it throws an invalid_argument exception.
Syntax
Following is the syntax for std::string::stoull() function.
unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10); unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
Parameters
- str − It indicates the string object with the representation of a integral number.
- idx − It indicates the pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
- base − It indicates the numerical base that determines the valid character and their interpretation.
Return value
It returns a string as the value into a unsigned long long integer.
Example 1
Following is the basic example to convert a string into an unsigned long long integer of std::stoull() function using C++.
#include <iostream> #include <string> using namespace std; int main() { string number = "12345678901234567890"; unsigned long long ull = stoull(number); cout << "The number = " << ull << endl; return 0; }
Output
If we run the above code it will generate the following output.
The number = 12345678901234567890
Example 2
Following is an another example specifying the numeric base to convert a hexadecimal string to its decimal equivalent with base 16.
#include <iostream> #include <string> using namespace std; int main() { string hexNumber = "1A2B3C4D5E6F"; unsigned long long ull = stoull(hexNumber, nullptr, 16); cout << "The hexadecimal number " << hexNumber << " is " << ull << " in decimal." << endl; return 0; }
Output
If we run the above code it will generate the following output.
The hexadecimal number 1A2B3C4D5E6F is 28772997619311 in decimal.
Example 3
In this program we are extracting the first number from a string that contains both text and numbers, and also demonstrating how to use the idx parameter to find the position of the character following the parsed number.
#include <iostream> #include <string> using namespace std; int main() { string mixedString = "42ABC!@"; size_t idx = 0; while (idx < mixedString.size() && !isdigit(mixedString[idx])) { ++idx; } if (idx < mixedString.size()) { unsigned long long ull = stoull(mixedString.substr(idx), & idx, 10); cout << "Extracted number = " << ull << endl; idx += mixedString.find_first_of("0123456789"); cout << "Next character in string after number = " << mixedString[idx] << endl; } return 0; }
Output
Following is the output of the above code.
Extracted number = 42 Next character in string after number = A
Example 4
In this program we convert a string that does not represent a number, catching and handling the potential exceptions might throw.
#include <iostream> #include <string> using namespace std; int main() { string number = "not a number"; try { unsigned long long ull = stoull(number); cout << "The number is: " << ull << std::endl; } catch (const invalid_argument & ia) { cerr << "Invalid argument = " << ia.what() << endl; } return 0; }
Output
Following is the output of the above code.
Invalid argument: stoull