📜  C++ strtoull()

📅  最后修改于: 2020-09-25 09:01:33             🧑  作者: Mango

C++中的strtoull() 函数将字符串的内容解释为指定基数的整数,并以无符号long long int形式返回其值。

C++中的strtoull() 函数将字符串的内容解释为指定基数的整数,并以无符号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() 函数将字符串,指向字符的指针和整数值-base作为其参数,将字符串的内容解释为给定base的整数,并返回unsigned long long int值。

此函数在头文件中定义。

strtoull()参数

strtoull()返回值

strtoull() 函数返回:

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

#include 
#include 
#include 
 
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() 函数的有效整数值包括:

如果参数的开头包含减号(-),则负数将隐式转换为无符号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 
#include 
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 
#include 
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

如果基数为``0'',则通过查看字符串格式自动确定数字基数。如果前缀为0,则基数为八进制(8)。如果前缀为0x或0X,则基数为十六进制(16),否则基数为十进制(10)。

示例4:以0为底的strtoull() 函数

#include 
#include 
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