C/C中的strtoul()函数++

这个 斯特图尔() C/C++中的函数,该函数根据给定的基数将str中字符串的初始部分转换为无符号长整型值,该基数必须介于2和36之间,或者是特殊值0。此函数将丢弃所有空白字符,直到找到第一个非空白字符,然后尽可能多地使用字符来形成有效的base-n无符号整数表示形式,并将其转换为整数值。

null

语法:

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

参数: 该函数接受三个强制性参数,如下所述:

  • str: 指向要解释的以null结尾的字节字符串的指针
  • 完: 指向字符指针的指针(指向char*类型的对象的引用)
  • 基数: 解释的整数值的基

返回值: 该函数返回两个值,如下所示:

  • 如果成功,它将返回一个与str的内容相对应的整数值。
  • 如果未进行有效转换,则返回0。

以下程序说明了上述功能:

项目1:

C++

// C++ program to illustrate the
// strtoul() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initializing 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 : 15124320String in str is :  Geeks For Geeks

项目2:

C++

// C++ program to illustrate the
// strtoul() function with
// different bases
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initializing the string
char str[256] = "12345 GFG" ;
// reference pointer
char * end;
long result;
// finding the unsigned long integer
// 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 integer
// 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 integer
// 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 : 12345String in str is :  GFGThe unsigned long integer is : 24677String in str is :  GFGThe unsigned long integer is : 866825String in str is :  GFG

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享