In C++, std::stoul() converts a string to unsigned 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::stoul(...).
Syntax
Pass unsigned-looking text.
C++
unsigned long n = std::stoul("255");
Call
What it does
std::stoul(str)
Parse as unsigned long.
For 64-bit unsigned values prefer stoull when needed.
Library declarations
C++
unsigned long stoul(const string& str, size_t* idx = 0, int base = 10);
Parameters
Parameter
Description
str
Input.
idx, base
Optional.
Return Value
Return Value
Description
unsigned long
Parsed value.
C++ Examples
Example 1 — Basic stoul()
Convert to unsigned long.
C++
#include <iostream>
#include <string>
int main() {
unsigned long n = std::stoul("255");
std::cout << n << '\\n';
return 0;
}
Output
255
How It Works
Digits parse as base 10.
Type is unsigned long.
Prints 255.
Example 2 — stoul Hex
Parse hex color-like text.
C++
#include <iostream>
#include <string>
int main() {
unsigned long n = std::stoul("ff", nullptr, 16);
std::cout << n << '\\n';
return 0;
}
Output
255
How It Works
Base 16 reads ff.
Value is 255.
Shown in decimal.
Example 3 — stoul idx
Stop at non-digit.
C++
#include <iostream>
#include <string>
int main() {
std::size_t i = 0;
unsigned long n = std::stoul("10px", &i);
std::cout << n << ' ' << i << '\\n';
return 0;
}