C++ std::stoull() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::stoull

Definition

In C++, std::stoull() converts a string to unsigned long long.

Include <string>.

This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::stoull(...).

Syntax

Pass the text.

C++
unsigned long long n = std::stoull("18446744073709551615");
CallWhat it does
std::stoull(str)Parse as unsigned long long.

Use for large unsigned IDs.

Library declarations
C++
unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);

Parameters

ParameterDescription
strInput.
idx, baseOptional.

Return Value

Return ValueDescription
unsigned long longParsed value.

C++ Examples

Example 1 — Basic stoull()

Convert a large unsigned text.

C++
#include <iostream>
#include <string>

int main() {
    unsigned long long n = std::stoull("10000000000");
    std::cout << n << '\\n';
    return 0;
}

How It Works

  1. Value exceeds 32-bit range.
  2. stoull holds it.
  3. Printed in full.

Example 2 — Base 2

Parse a binary string.

C++
#include <iostream>
#include <string>

int main() {
    auto n = std::stoull("1010", nullptr, 2);
    std::cout << n << '\\n';
    return 0;
}

How It Works

  1. Base 2 reads bits.
  2. 1010 binary is ten.
  3. Output is 10.

Example 3 — stoull Throws

Catch invalid input.

C++
#include <iostream>
#include <string>

int main() {
    try {
        std::stoull("nope");
    } catch (const std::invalid_argument&) {
        std::cout << "bad\\n";
    }
    return 0;
}

How It Works

  1. No convertible prefix.
  2. Exception fires.
  3. Handler prints bad.

Frequently Asked Questions

String to unsigned long long.
stoll.
<string>.
Yes, 2–36.
Yes on invalid/out of range.

Next: to_string()

Keep going through the string functions. Convert a number to std::string.

to_string() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful