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

📅  最后修改于: 2021-05-30 12:16:49             🧑  作者: Mango

C / C++中的strtoul()函数根据给定的基数将str中字符串的初始部分转换为无符号的long int值,该值必须在2到36之间(包括2和36),或者是特殊值0。此函数丢弃任何空白字符,直到找到第一个非空白字符,然后再使用尽可能多的字符来形成有效的以n为基数的无符号整数表示形式并将其转换为整数值。

句法:

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

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

  • str:指向要解释的以空值终止的字节字符串的指针
  • end:指向字符的指针(参考char *类型的对象)
  • base:解释后的整数值的基数

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

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

下面的程序说明了上述函数:

程序1:

C++
// C++ program to illustrate the
// strtoul() function
#include 
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;
}


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

程式2:

C++

// C++ program to illustrate the
// strtoul() function with
// different bases
#include 
using namespace std;
 
int main()
{
 
    // initiaizing 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 : 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”