wcstoul() function in C/C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

String value given is -> 999gfg
Unsigned Long Int value will be -> 559753324
End String will be->

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.