C++ strtoull()

C++ 中的 `strtoull()` 函数将字符串的内容解释为指定基数的整数,并将其值作为 unsigned long long int 类型返回。

该函数还会将一个指针设置为指向字符串中最后一个有效字符之后的第一个字符,如果不存在,则指针设置为 null。

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

strtoull() 原型 [截至 C++ 11 标准]

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

`strtoull()` 函数以字符串 `str`、一个指向字符的指针 `end` 和一个整数值 `base` 作为参数,将 `str` 的内容解释为给定基数的整数,并返回一个 unsigned long long int 值。

该函数定义在 `` 头文件中。


strtoull() 参数

  • str: 一个包含整数表示的字符串。
  • end: 一个指向已分配的 `char*` 类型对象的引用。函数会将 `end` 的值设置为 `str` 中最后一个有效字符之后的下一个字符。此参数也可以是空指针,在这种情况下不使用。
  • base: 整数值的基数。`base` 的有效值集合为 {0, 2, 3, …, 35, 36}。

strtoull() 返回值

`strtoull()` 函数返回

  • 一个 unsigned long long int 值(从字符串转换而来)。
  • 如果无法执行有效的转换,则返回 0。

示例 1:`strtoull()` 函数如何工作?

#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;
}

运行程序后,输出将是

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

`strtoull()` 函数的有效整数值包含:

  • 可选的正号 (+) 或负号 (-)。
  • 八进制基数的前缀 0(仅当 base = 8 或 0 时适用)。
  • 十六进制基数的前缀 0x 或 0X(仅当 base = 16 或 0 时适用)。
  • 数字和/或字母序列(如果 base 大于 10)。

如果参数以负号 (-) 开头,则负数会被隐式转换为 unsigned long long int 类型,该类型表示一个正数。

参数 `base` 的有效值为 {0, 2, 3, ..., 35, 36}。基数 2 的有效数字集为 {0, 1},基数 3 的有效数字集为 {0, 1, 2},依此类推。对于从 11 到 36 的基数,有效数字包括字母。基数 11 的有效数字集为 {0, 1, …, 9, A, a},基数 12 的有效数字集为 {0, 1, …, 9, A, a, B, b},依此类推。

示例 2:不同基数的 `strtoull()` 函数

#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;
}

运行程序后,输出将是

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 =

`strtoull()` 函数会忽略所有前导的空白字符,直到找到第一个非空白字符。

通常,`strtoull()` 函数的有效整数参数具有以下形式:

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

然后,从该字符开始,尽可能多地读取构成有效整数表示的字符,并将它们转换为 long long int 值。最后一个有效字符之后剩余的字符串将被忽略,并且对结果没有影响。

示例 3:`strtoull()` 函数处理前导空白、负号和无效转换

#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;
}

运行程序后,输出将是

 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

如果 `base` 为 0,则数值基数将根据字符串的格式自动确定。如果前缀是 0,则基数为八进制 (8)。如果前缀是 0x 或 0X,则基数为十六进制 (16),否则基数为十进制 (10)。

示例 4:`base` 为 0 的 `strtoull()` 函数

#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;
}

运行程序后,输出将是

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

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战