C++ String::stoul() function



The C++ std::string::stoul() function is used to convert a string to an unsigned long integer. It reads the string, interprets it as an unsigned long integer, and optionally allows specifying the base. If the conversion gets failed, it throws an invalid_argument exception.

Syntax

Following is the syntax for std::string::stoul() function.

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);

Parameters

  • str − It indicates the string object with the representation of a integral number.
  • idx − It indicates the pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
  • base − It indicates the numerical base that determines the valid character and their interpretation.

Return value

It returns a string as the value into a unsigned long value.

Example 1

Following is the basic example of how the std::stoul function is used in C++ to convert a string to an unsigned long integer.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string str = "123456";
   unsigned long number = stoul(str);
   cout << "The string \"" << str << "\" converted to unsigned long is " << number << endl;
   return 0;
}

Output

If we run the above code it will generate the following output.

The string "123456" converted to unsigned long is 123456

Example 2

In this example, we are passing a hexadecimal colour to convert that code string into its numeric RGB(red, green, blue) value.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string hexColor = "FF5733";
   unsigned long rgbValue = stoul(hexColor, nullptr, 16);
   cout << "The numeric RGB value = " << rgbValue << std::endl;
   return 0;
}

Output

If we run the above code it will generate the following output.

The numeric RGB value = 16734003

Example 3

In this program we intialized two strings to compare the numerical strings that determine which one represents a larger number.

#include <iostream>
#include <string>
int main() {
   std::string numStr1 = "4294967295";
   std::string numStr2 = "123456789";
   unsigned long num1 = std::stoul(numStr1);
   unsigned long num2 = std::stoul(numStr2);
   if (num1 > num2) {
      std::cout << numStr1 << " is greater than " << numStr2 << std::endl;
   } else {
      std::cout << numStr2 << " is greater than " << numStr1 << std::endl;
   }
   return 0;
}

Output

Following is the output of the above code.

4294967295 is greater than 123456789

Example 4

In this program, we are validating and Converting Command-Line parameters. So, ensures they are valid unsigned long integers.

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[]) {
   if (argc < 2) {
      cerr << "Usage: " << argv[0] << " <number>" << endl;
      return 1;
   }
   try {
      unsigned long param = std::stoul(argv[1]);
      cout << "The command-line parameter is: " << param << endl;
   } catch (const exception & e) {
      cerr << "Error: " << e.what() << endl;
      return 1;
   }
   return 0;
}

Output

Following is the output of the above code.

Usage: main <number>             
string.htm
Advertisements