Use c_str().

strtol(string.c_str() + 10, NULL, 10);
Answer from timrau on Stack Overflow
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › strtol
std::strtol, std::strtoll - cppreference.com
June 6, 2023 - Interprets an integer value in a byte string pointed to by str · Discards any whitespace characters (as identified by calling std::isspace) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number ...
🌐
Cplusplus
cplusplus.com › reference › cstdlib › strtol
strtol - Convert string to long integer
Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number. The function first discards as many ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › strol-function-in-c
strol() function in C++ - GeeksforGeeks
November 21, 2021 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
cppreference.com
en.cppreference.com › c › string › byte › strtol
strtol, strtoll - cppreference.com
December 27, 2024 - Interprets an integer value in a byte string pointed to by str · Discards any whitespace characters (as identified by calling isspace) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation ...
🌐
Medium
medium.com › @simontoth › daily-bit-e-of-c-std-stoi-std-stol-std-stoll-std-stoul-std-stoul-std-stof-189b7243f674
Daily bit(e) of C++ | std::stoi, std::stol, std::stoll, std::stoul, std::stoul, std::stof, std::stod, std::stold | by Šimon Tóth | Medium
September 20, 2023 - C++11 functions for converting std::string to integer and floating point types: std::stoi, std::stol, std::stoll, std::stoul, std::stoul, std::stof, std::stod, std::stold.
🌐
cppreference.com
en.cppreference.com › cpp › string › basic_string › stol
std::stoi, std::stol, std::stoll - cppreference.com
August 7, 2023 - Interprets a signed integer value in the string str · Let ptr be an internal (to the conversion functions) pointer of type char* (1,3,5) or wchar_t* (2,4,6), accordingly
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strtol.htm
C library - strtol() function
The C stdlib library strtol() function is used to convert a string to a long integer number according to the given base. which must be lies between 2 and 32 inclusive, or be the special value 0.
🌐
OpenSUSE Manpages
manpages.opensuse.org › tumbleweed › stdman › std::strtol,std::strtoll(3)
std::strtol,std::strtoll(3) — stdman
July 8, 2024 - Scroll to navigation · std::strtol,std::strtoll - std::strtol,std::strtoll
Find elsewhere
🌐
Programiz
programiz.com › cpp-programming › library-function › cstdlib › strtol
C++ strtol() - C++ Standard Library
The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.
🌐
Substack
simontoth.substack.com › daily bit(e) of c++ › daily bit(e) of c++ | std::stoi, std::stol, std::stoll, std::stoul, std::stoul, std::stof, std::stod, std::stold
Daily bit(e) of C++ | std::stoi, std::stol, std::stoll, std::stoul, std::stoul, std::stof, std::stod, std::stold
September 20, 2023 - Daily bit(e) of C++ #262, Functions for converting std::string to integer and floating point types: std::stoi, std::stol, std::stoll, std::stoul, std::stoul, std::stof, std::stod, std::stold.
Top answer
1 of 4
35

The first argument is the string. It has to be passed in as a C string, so if you have a std::string use .c_str() first.

The second argument is optional, and specifies a char * to store a pointer to the character after the end of the number. This is useful when converting a string containing several integers, but if you don't need it, just set this argument to NULL.

The third argument is the radix (base) to convert. strtol can do anything from binary (base 2) to base 36. If you want strtol to pick the base automatically based on prefix, pass in 0.

So, the simplest usage would be

long l = strtol(input.c_str(), NULL, 0);

If you know you are getting decimal numbers:

long l = strtol(input.c_str(), NULL, 10);

strtol returns 0 if there are no convertible characters at the start of the string. If you want to check if strtol succeeded, use the middle argument:

const char *s = input.c_str();
char *t;
long l = strtol(s, &t, 10);
if(s == t) {
    /* strtol failed */
}

If you're using C++11, use stol instead:

long l = stol(input);

Alternately, you can just use a stringstream, which has the advantage of being able to read many items with ease just like cin:

stringstream ss(input);
long l;
ss >> l;
2 of 4
4

Suppose you're given a string char const * str. Now convert it like this:

#include <cstdlib>
#include <cerrno>

char * e;
errno = 0;

long n = std::strtol(str, &e, 0);

The last argument 0 determines the number base you want to apply; 0 means "auto-detect". Other sensible values are 8, 10 or 16.

Next you need to inspect the end pointer e. This points to the character after the consumed input. Thus if all input was consumed, it points to the null-terminator.

if (*e != '\0') { /* error, die */ }

It's also possible to allow for partial input consumption using e, but that's the sort of stuff that you'll understand when you actually need it.

Lastly, you should check for errors, which can essentially only be overflow errors if the input doesn't fit into the destination type:

if (errno != 0) { /* error, die */ }

In C++, it might be preferable to use std::stol, though you don't get to pick the number base in this case:

#include <string>

try { long n = std::stol(str); }
catch (std::invalid_argument const & e) { /* error */ }
catch (std::out_of_range const & e)     { /* error */ }
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdstol-and-stdstoll-functions-in-c
std::stol() and std::stoll() Functions in C++ - GeeksforGeeks
July 11, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › strtol-function-in-c-stl
strtol() function in C++ STL - GeeksforGeeks
June 1, 2022 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cstdlib › strtol
C++ cstdlib strtol() - Convert String to Long | Vultr Docs
November 14, 2024 - Click below to sign up and get $250 of credit to try our products over 30 days · The strtol() function in C++ is a part of the cstdlib library, designed to convert strings to a long integer (long). This utility is especially valuable when dealing with user input or parsing strings to extract ...
🌐
University of Chicago
naipc.uchicago.edu › 2014 › ref › cppreference › en › cpp › string › basic_string › stol.html
std::stoi, std::stol, std::stoll - cppreference.com
Interprets a signed integer value in the string str · Function discards any whitespace characters (as identified by calling isspace()) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid base-n (where n=base) integer number representation ...
🌐
Cleveland Clinic
my.clevelandclinic.org › health › diseases › 9138-sexually-transmitted-diseases--infections-stds--stis
Sexually Transmitted Infections (Sexually Transmitted Diseases)
August 24, 2023 - Sexually transmitted infections (sexually transmitted diseases) are infections you can develop after engaging in sexual activities. Some cures exist for certain STIs.
🌐
cppreference.com
en.cppreference.com › cpp › string › basic_string › stof
std::stof, std::stod, std::stold - cppreference.com
August 7, 2023 - Interprets a floating point value in a string str · Let ptr be an internal (to the conversion functions) pointer of type char* (1,3,5) or wchar_t* (2,4,6), accordingly
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdstod-stdstof-stdstold-c
std::stod, std::stof, std::stold in C++ - GeeksforGeeks
October 29, 2018 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.