C /C++中的strtoul()函数根据给定的基数将str中字符串的初始部分转换为无符号的long int值,该值必须在2到36之间(包括2和36),或者是特殊值0。此函数将丢弃所有白色空格字符,直到找到第一个非空格字符,然后再使用尽可能多的字符来形成有效的base-n无符号整数表示形式并将其转换为整数值。
用法:
unsigned long int strtoul(const char *str, char **end, int base)
参数:该函数接受三个强制性参数,如下所述:
- str: 指向要解释的以空终止的字节字符串的指针
- end :指向字符的指针(指向char *类型的对象)
- base :解释后的整数值的基数
返回值:该函数返回两个值,如下所示:
- 成功时,它将返回一个与str内容相对应的整数值。
- 如果没有完成有效的转换,则返回0。
以下示例程序旨在说明上述函数:
示例1:
// C++ program to illustrate the
// strtoul() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initiaizing the string
char str[256] = "90600 Geeks For Geeks";
// reference pointer
char* end;
long result;
// finding the unsigned long
// integer with base 36
result = strtoul(str, &end, 36);
// printing the unsigned number
cout << "The unsigned long integer is : "
<< result << endl;
cout << "String in str is : " << end;
return 0;
}
输出:
The unsigned long integer is : 15124320 String in str is : Geeks For Geeks
示例2:
// C++ program to illustrate the
// strtoul() function with
// different bases
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initiaizing the string
char str[256] = "12345 GFG";
// reference pointer
char* end;
long result;
// finding the unsigned long interger
// with base 36
result = strtoul(str, &end, 0);
cout << "The unsigned long integer is : "
<< result << endl;
cout << "String in str is : " << end << endl;
// finding the unsigned long interger
// with base 12
result = strtoul(str, &end, 12);
cout << "The unsigned long integer is : "
<< result << endl;
cout << "String in str is : " << end << endl;
// finding the unsigned long interger
// with base 30
result = strtoul(str, &end, 30);
cout << "The unsigned long integer is : "
<< result << endl;
cout << "String in str is : " << end << endl;
return 0;
}
输出:
The unsigned long integer is : 12345 String in str is : GFG The unsigned long integer is : 24677 String in str is : GFG The unsigned long integer is : 866825 String in str is : GFG
相关用法
- C++ div()用法及代码示例
- C++ fma()用法及代码示例
- C++ log()用法及代码示例
- C++ regex_iterator()用法及代码示例
- C++ isunordered()用法及代码示例
- C++ map key_comp()用法及代码示例
- C++ real()用法及代码示例
- C++ imag()用法及代码示例
- C++ valarray pow()用法及代码示例
- C++ valarray log()用法及代码示例
注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 strtoul() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。