C /C++中的wcstoul()函數將寬字符串轉換為無符號長整數。
此函數將指針設置為指向寬字符串的最後一個有效字符之後的第一個字符(如果存在),否則,該指針將設置為null。此函數將忽略所有前導空白字符,直到找到主要的非空白字符為止。
用法:
unsigned long wcstoul( const wchar_t* string, wchar_t** endString, int base )
參數:該函數接受三個強製性參數,如下所述:
- string: 指定包含整數表示形式的字符串。
- endString: 指定函數將endString的值設置為字符串中最後一個有效字符之後的下一個字符。
- base: 指定基數的有效值集為{0,2,3,…,35,36}。
返回值:該函數返回兩個值,如下所示:
- 成功後,函數將轉換後的整數返回為無符號long int值。
- 如果沒有有效的轉換,則返回零。
以下示例程序旨在說明上述函數:
示例1:
// C++ program to illustrate
// wcstoul() function
// with base equal to 36
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize the wide string
wchar_t string[] = L"999gfg";
// set a pointer ponting
// the string at the end
wchar_t* endString;
// print the unsigned long integer value
// with the end string
// initialize the base as 36
unsigned long value = wcstoul(string, &endString, 36);
wcout << L"String value given is -> "
<< string << endl;
wcout << L"Unsigned Long Int value will be -> "
<< value << endl;
wcout << L"End String will be-> "
<< endString << endl;
return 0;
}
輸出:
String value given is -> 999gfg Unsigned Long Int value will be -> 559753324 End String will be->
示例2:
// C++ program to illustrate
// wcstoul() function
// with different bases
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize the wide string
wchar_t string[] = L"99999999999gfg";
// set a pointer ponting
// the string at the end
wchar_t* endString;
// print the unsigned long integer value with
// the end string with base 36
long value = wcstol(string, &endString, 35);
wcout << L"String value --> " << string << "\n";
wcout << L"Long integer value --> " << value << "\n";
wcout << L"End String = " << endString << "\n";
// print the unsigned long integer value with
// the end string with base 16
value = wcstol(string, &endString, 16);
wcout << L"String value --> " << string << "\n";
wcout << L"Long integer value --> " << value << "\n";
wcout << L"End String = " << endString << "\n";
// print the unsigned long integer value with
// the end string with base 12
value = wcstol(string, &endString, 12);
wcout << L"String value --> " << string << "\n";
wcout << L"Long integer value --> " << value << "\n";
wcout << L"End String = " << endString << "\n";
return 0;
}
輸出:
String value --> 99999999999gfg Long integer value --> 9223372036854775807 End String = String value --> 99999999999gfg Long integer value --> 10555311626649 End String = gfg String value --> 99999999999gfg Long integer value --> 607915939653 End String = gfg
相關用法
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ real()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ valarray tan()用法及代碼示例
- C++ valarray pow()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wcstoul() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。