C++ strtoull()

The strtoull() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as an unsigned long long int.

This function also sets a pointer to point to the first character after the last valid character of the string if there is any, otherwise the pointer is set to null.

For base 10 and the string "41aac"
 
Valid numeric part -> 42
First character after valid numeric part -> a

strtoull() prototype [As of C++ 11 standard]

unsigned long long int strtoull(const char* str, char** end, int base);

The strtoull() function takes string, a pointer to character and an integer value - base as its parameter, interprets the content of string as an integral number of the given base and returns an unsigned long long int value.

This function is defined in <cstdlib> header file.


strtoull() Parameters

  • str: A string having the representation of an integral number.
  • end: Reference to an already allocated object of type char*. The value of end is set by the function to the next character in str after the last valid character. This parameter can also be a null pointer, in which case it is not used.
  • base: The base of the integral value. The set of valid values for base is {0, 2, 3, …, 35, 36}.

strtoull() Return value

The strtoull() function returns:

  • an unsigned long long int value (which is converted from the string).
  • 0 if no valid conversion could be performed.

Example 1: How strtoull() function works?

#include <iostream>
#include <cstdlib>
#include <cstring>
 
using namespace std;
 
int main()
{
 	int base = 10;
 	char numberString[] = "231ax12";
 	char *end;
 	unsigned long long int number;
 	
 	number = strtoull(numberString, &end, base);
 	cout << "String value = " << numberString << endl;
 	cout << "Unsigned Long long int value = " << number << endl;
 	cout << "End String = " << end << endl;
 	
 	strcpy(numberString, "231");
 	cout << "String value = " << numberString << endl;
 	number = strtoull(numberString, &end, base);
 	cout << "Unsigned Long long int value = " << number << endl;
 	if (*end) {
      	cout << end;
 	} else {
      	cout << "Null pointer";
 	}
 	return 0;
}

When you run the program, the output will be:

String value = 231ax12
Unsigned Long long int value = 231
End String = ax12
String value = 231
Unsigned Long long int value = 231
Null pointer

A valid integer value for strtoull() function consists of:

  • An optional + or - sign.
  • A prefix 0 for octal base (applies only when base = 8 or 0).
  • A prefix 0x or 0X for hexadecimal base (applies only when base = 16 or 0).
  • A sequence of digits and/or alphabets (if base is greater than 10).

If the argument contains a minus (-) sign at the beginning, the negative number is implicitly converted to a unsigned long long int type which is a positive number.

The valid values for parameter base is {0, 2, 3, ..., 35, 36}. A set of valid digits for base 2 is {0, 1}, for base 3 is {0, 1, 2} and so on. For bases starting from 11 to 36, valid digits include alphabets. The set of valid digits for base 11 is {0, 1, …, 9, A, a}, for base 12 is {0, 1, …, 9, A, a, B, b} and so on.

Example 2: strtoull() function with different bases

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{
 	char *end;
 	
 	cout << "148ax" << " to Unsigned Long Long Int with base-5 = " << strtoll("148ax", &end, 5) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	cout << "148ax" << " to Unsigned Long Long Int with base-15 = " << strtoll("148ax", &end, 15) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	cout << "148ax" << " to Unsigned Long Long Int with base-35 = " << strtoll("148ax", &end, 35) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	return 0;
}

When you run the program, the output will be:

148ax to Unsigned Long Long Int with base-5 = 9
End String = 8ax
 
148ax to Unsigned Long Long Int with base-15 = 4405
End String = x
 
148ax to Unsigned Long Long Int with base-35 = 1682308
End String =

The strtoull() function ignores all the leading whitespace characters until the primary non-whitespace character is found.

In general, a valid integer argument for strtoull() function has the following form:

[whitespace] [- | +] [0 | 0x] [alphanumeric characters]

Then, beginning from this character, it takes as many characters as possible that forms a valid integer representation and converts them to a long long int value. Whatever is left of the string after the last valid character is ignored and has no effect on the result.

Example 3: strtoull() function for leading whitespace, minus and invalid conversion

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{
 	char *end;
 	
 	cout << "  25axbz" << " to Unsigned Long Long Int with base-11 = " << strtoull("  25axbz", &end, 11) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	/* Negative value is converted to unsigned long long int type */
 	cout << "   -110bcd" << " to Unsigned Long Long Int with base-2 = " << strtoull("   -110bcd", &end, 2) << endl;
 	cout << "End String = " << end << endl << endl;
 
 	cout << "ax110.97" << " to Unsigned Long Long Int with base-10 = " << strtoull("ax110.97", &end, 10) << endl;
 	cout << "End String = " << end << endl << endl;
 
 	return 0;
}

When you run the program, the output will be:

 25axbz to Unsigned Long Long Int with base-11 = 307
End String = xbz
 
  -110bcd to Unsigned Long Long Int with base-2 = 18446744073709551610
End String = bcd
 
ax110.97 to Unsigned Long Long Int with base-10 = 0
End String = ax110.97

If the base is ​0​, the numeric base is determined automatically by looking at the format of the string. If the prefix is 0, the base is octal (8). If the prefix is 0x or 0X, the base is hexadecimal (16), otherwise the base is decimal (10).

Example 4: strtoull() function with base 0

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{
 	char *end;
 	
 	/* octal base */
 	cout << "017x" << " to Unsigned Long Long Int with base-0 = " << strtoull("017x", &end, 0) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	/* hexadecimal base */
 	cout << "0x1cg" << " to Unsigned Long Long Int with base-0 = " << strtoull("0x1cg", &end, 0) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	/* decimal base */
 	cout << "70sxz" << " to Unsigned Long Long Int with base-0 = " << strtoull("70sxz", &end, 0) << endl;
 	cout << "End String = " << end << endl << endl;
 	
 	return 0;
}

When you run the program, the output will be:

017x to Unsigned Long Long Int with base-0 = 15
End String = x
 
0x1cg to Unsigned Long Long Int with base-0 = 28
End String = g
 
70sxz to Unsigned Long Long Int with base-0 = 70
End String = sxz

Also Read:

Did you find this article helpful?