📅  最后修改于: 2020-09-25 09:00:41             🧑  作者: Mango
此函数还将设置一个指针,以指向该字符串的最后一个有效字符之后的第一个字符 (如果存在),否则将其设置为null。
For base 10 and the string "201xz$"
Valid numeric part -> 201
First character after valid numeric part -> x
long long int strtoll(const char* str, char** end, int base);
strtoll() 函数将字符串,指向字符的指针和整数值-base作为其参数,将字符串的内容解释为给定base的整数,并返回long long int值。
此函数在
strtoll() 函数返回:
#include
#include
#include
using namespace std;
int main()
{
int base = 10;
char numberString[] = "13.5ab_1x";
char *end;
long long int number;
number = strtoll(numberString, &end, base);
cout << "String value = " << numberString << endl;
cout << "Long long int value = " << number << endl;
cout << "End String = " << end << endl;
strcpy(numberString, "13");
cout << "String value = " << numberString << endl;
number = strtoll(numberString, &end, base);
cout << "Long long int value = " << number << endl;
if (*end) {
cout << end;
} else {
cout << "Null pointer";
}
return 0;
}
运行该程序时,输出为:
String value = 13.5ab_1x
Long long int value = 13
End String = .5ab_1x
String value = 13
Long long int value = 13
Null pointer
strtoll() 函数的有效整数值包括:
参数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},依此类推。
#include
#include
using namespace std;
int main()
{
char *end;
cout << "23ajz" << " to Long Long Int with base-7 = " << strtoll("23ajz", &end, 7) << endl;
cout << "End String = " << end << endl << endl;
cout << "23ajz" << " to Long Long Int with base-20 = " << strtoll("23ajz", &end, 20) << endl;
cout << "End String = " << end << endl << endl;
cout << "23ajz" << " to Long Long Int with base-36 = " << strtoll("23ajz", &end, 36) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
23ajz to Long Long Int with base-7 = 17
End String = ajz
23ajz to Long Long Int with base-20 = 17419
End String = z
23ajz to Long Long Int with base-36 = 3512879
End String =
直到主非空白字符被找到与strtoll() 函数将忽略所有的前导空白字符 。
通常,strtoll() 函数的有效整数参数具有以下形式:
[whitespace] [- | +] [0 | 0x] [alphanumeric characters]
然后,从这个字符开始,它需要尽可能多的字符 ,以形成有效的整数表示形式并将其转换为long long int值。最后一个有效字符之后的字符串剩余部分将被忽略,并且对结果没有任何影响。
#include
#include
using namespace std;
int main()
{
char *end;
cout << " 25axbz" << " to Long Long Int with base-11 = " << strtoll(" 25axbz", &end, 11) << endl;
cout << "End String = " << end << endl << endl;
cout << " 110bcd" << " to Long Long Int with base-2 = " << strtoll(" 110bcd", &end, 2) << endl;
cout << "End String = " << end << endl << endl;
cout << "ax110.97" << " to Long Long Int with base-10 = " << strtoll("ax110.97", &end, 10) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
25axbz to Long Long Int with base-11 = 307
End String = xbz
110bcd to Long Long Int with base-2 = 6
End String = bcd
ax110.97 to Long Long Int with base-10 = 0
End String = ax110.97
如果基数为``0'',则通过查看字符串格式自动确定数字基数。如果前缀为0,则基数为八进制(8)。如果前缀为0x或0X,则基数为十六进制(16),否则基数为十进制(10)。
#include
#include
using namespace std;
int main()
{
char *end;
/* octal base */
cout << "025x" << " to Long Long Int with base-0 = " << strtoll("025x", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
/* hexadecimal base */
cout << "0xf1x" << " to Long Long Int with base-0 = " << strtoll("0xf1x", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
/* decimal base */
cout << "15ab" << " to Long Long Int with base-0 = " << strtoll("15ab", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
025x to Long Long Int with base-0 = 21
End String = x
0xf1x to Long Long Int with base-0 = 241
End String = x
15ab to Long Long Int with base-0 = 15
End String = ab