Skip to content
Related Articles

Related Articles

Improve Article

wcstoul() function in C/C++

  • Last Updated : 27 Jul, 2021

The wcstoul() function in C/C++ convert wide string to unsigned long integer
This function sets a pointer to point to the first character after the last valid character of the wide string if there is any, otherwise, the pointer is set to null. This function ignores all the leading whitespace characters until the primary non-whitespace character is found.

Syntax:  

unsigned long wcstoul( const wchar_t* string, wchar_t** endString, int base )

Parameters: The function accepts three mandatory parameters which are described below:  

  • string: specifies the string containing the representation of an integral number.
  • endString: specifies the value of endString is set by the function to the next character in string after the last valid character.
  • base: specifies the set of valid values for base is {0, 2, 3, …, 35, 36}.

Return value: The function returns two value as below:  



  • On success, the function returns the converted integral number as an unsigned long int value.
  • returns zero, if no valid conversion takes place.

Below programs illustrate the above function: 

Program 1:  

C++




// 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 pointing
    // 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;
}
Output: 
String value given is -> 999gfg
Unsigned Long Int value will be -> 559753324
End String will be->

 

Program 2: 

C++




// 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 pointing
    // 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;
}
Output: 
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

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :