Introduction
In this blog we will learn about std::stoul and std::stoull functions in C++. You can use std::stoul function to convert a string to an unsigned long value. Also, you can use std::stoull function to convert a string to an unsigned long long value. Let’s see how these functions work.
What is std:: stoul in c++?
It converts the string to an unsigned long value. It parses the string that has been provided and interprets its content as the integral number as per the specified base. And finally returns the unsigned long integer value.
Syntax:
unsigned long stoul(const string &str, size_t* p, int base);
Parameters:
Str:- String object
P:- pointer of an object
Base:- Determination of the interpretation and the valid characters, the default value is 10.
Implementation in C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Hexadecimal string s1
string s1 = "FF";
// Converted unsigned long
unsigned long res1 = stoul(s1, nullptr, 16);
// Printing the result res1
cout << res1 << "\n";
// Hexadecimal string s2
string s2 = "3f11f27";
// Converted unsigned long
unsigned long long res2 = stoul(s2, nullptr, 16);
// Printing the result res2
cout << res2;
return 0;
}
Output:
255
66133799