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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。