C++ 中的 strtol() 函数将 字符串 的内容解释为指定进制的整数,并返回其 long int 值。该函数还会设置一个指针,使其指向字符串中最后一个有效字符之后的第一个字符,如果不存在,则指针被设为 null。
For base 10 and the string "12abc": Valid numeric part -> 12 First character after valid numeric part -> a
strtol() 原型 [C++ 11 标准]
long int strtol(const char* str, char** end, int base);
strtol() 函数以字符串、指向字符的指针和整数值 base 作为参数,将字符串的内容解释为给定进制的整数,并返回一个 long int 值。
该函数定义在 `
strtol() 参数
str:
一个包含整数表示的字符串。end:
char* 类型已分配对象的引用。函数将 end 的值设置为 str 中最后一个有效字符之后的下一个字符。此参数也可以是空指针,在这种情况下不使用。base:
整数值的进制。base 的有效值集合是 {0, 2, 3, …, 35, 36}。
strtol() 返回值
strtol() 函数返回
- 一个 long int 值(从字符串转换而来)。
- 如果无法执行有效转换,则返回 0。
示例 1:C++ 中 strtol() 的工作原理?
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int base = 10;
char str[] = "27ab_1x";
char *end;
long int num;
num = strtol(str, &end, base);
cout << "Number in String = " << str << endl;
cout << "Number in Long Int = " << num << endl;
cout << "End String = " << end << endl << endl;
// the pointer to invalid characters can be null
strcpy(str, "27");
cout << "Number in String = " << str << endl;
num = strtol(str, &end, base);
cout << "Number in Long Int = " << num << endl;
if (*end) {
cout << end;
} else {
cout << "Null pointer";
}
return 0;
}
运行程序后,输出将是
Number in String = 27ab_1x Number in Long Int = 27 End String = ab_1x Number in String = 27 Number in Long Int = 27 Null pointer
strtol() 函数的有效整数值包括
- 可选的正号或负号。
- 八进制基数的 0 前缀(仅当 base = 8 或 0 时适用)。
- 十六进制基数的 0x 或 0X 前缀(仅当 base = 16 或 0 时适用)。
- 数字和/或字母序列(如果 base 大于 10)。
参数 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:不同进制下的 strtol() 函数
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
char *end;
cout << "128bz" << " to Long Int with base-5 = " << strtol("128bxz", &end, 5) << endl;
cout << "End String = " << end << endl << endl;
cout << "128bz" << " to Long Int with base-12 = " << strtol("128bxz", &end, 12) << endl;
cout << "End String = " << end << endl << endl;
cout << "128bz" << " to Long Int with base-36 = " << strtol("128bxz", &end, 36) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行程序后,输出将是
128bz to Long Int with base-5 = 7 End String = 8bxz 128bz to Long Int with base-12 = 2123 End String = xz 128bz to Long Int with base-36 = 64214135 End String =
strtol() 函数会忽略所有前导的空白字符,直到找到第一个非空白字符为止。
通常,strtol() 函数的有效整数参数具有以下形式
[whitespace] [- | +] [0 | 0x] [alphanumeric characters]
然后,从该字符开始,它会尽可能多地获取构成有效整数表示的字符,并将它们转换为 long int 值。最后一个有效字符之后的字符串剩余部分将被忽略,并且不会影响结果。
示例 3:strtol() 函数处理前导空白和无效转换
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char *end;
cout << " 25axbz" << " to Long Int with base-11 = " << strtol(" 25axbz", &end, 11) << endl;
cout << "End String = " << end << endl << endl;
cout << " 110bcd" << " to Long Int with base-2 = " << strtol(" 110bcd", &end, 2) << endl;
cout << "End String = " << end << endl << endl;
cout << "ax110.97" << " to Long Int with base-10 = " << strtol("ax110.97", &end, 10) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行程序后,输出将是
25axbz to Long Int with base-11 = 307 End String = xbz 110bcd to Long Int with base-2 = 6 End String = bcd ax110.97 to Long Int with base-10 = 0 End String = ax110.97
如果 base 为 0,则通过查看字符串的格式自动确定数字的基数。如果前缀是 0,则基数为八进制 (8)。如果前缀是 0x 或 0X,则基数为十六进制 (16),否则基数为十进制 (10)。
示例 4:base 为 0 的 strtol() 函数
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char *end;
/* octal base */
cout << "0128ai" << " to Long Int with base-0 = " << strtol("0128ai", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
/* hexadecimal base */
cout << "0x15axzz" << " to Long Int with base-0 = " << strtol("0x15axzz", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
/* decimal base */
cout << "23dfl" << " to Long Int with base-0 = " << strtol("23dfl", &end, 0) << endl;
cout << "End String = " << end << endl << endl;
return 0;
}
运行程序后,输出将是
0128ai to Long Int with base-0 = 10 End String = 8ai 0x15axzz to Long Int with base-0 = 346 End String = xzz 23dfl to Long Int with base-0 = 23 End String = dfl
另请阅读