Table of contents
1.
Introduction
2.
What is std:: stoul in c++?
2.1.
Implementation in C++
3.
What is std:: stoull in c++?
3.1.
Implementation in C++
4.
Frequently Asked Questions
4.1.
What is std::stoul in C++?
4.2.
What is std::stoull in C++?
4.3.
What do std::stoul and std::stoull functions do?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Std::stoull and std::stoul in Cpp

Author Ayush Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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);
You can also try this code with Online C++ Compiler
Run Code

 

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;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

255
66133799

What is std:: stoull in c++?

It converts the string to an unsigned long long value. It parses the string that has been provided and interprets its content as an integral number as per the specified base. And finally returns the unsigned long integer value.


Syntax:

unsigned long long stoull(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() {
  string str = "b3a73ce2ff2";

  //base = 16
  unsigned long long res = stoull(str, nullptr, 16);

  cout << "Given Hexadecimal Str = " << str << endl << "Str after applying std::stoull = " << res << endl;
  return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Given Hexadecimal Str = b3a73ce2ff2
Str after applying std::stoull = 12345678901234

Frequently Asked Questions

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 returns the unsigned long integer value.

What is std::stoull in C++?

It converts the string to an unsigned long long value. It parses the string that has been provided and interprets its content as the integral number as per the specified base, and returns the unsigned long integer value.

What do std::stoul and std::stoull functions do?

They convert the numbers represented or contained in a string to unsigned long values using std::stoul and unsigned long values using std::stoull.

Conclusion

This article is about the std:: stoul and std:: stoull functions in C++. We have discussed their syntax with code and their examples also.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, look at the interview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you enhance your knowledge regarding puzzles, and if you liked this blog, check other links. Do upvote our blog to help other ninjas grow. Happy Coding!"

Live masterclass