当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ wcstoul()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 wcstoul() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。