Open In App

wcstoul() function in C/C++

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The wcstoul() function in C/C++ converts a wide string to an unsigned long integer of the specified base. It 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. It 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 wide string containing the representation of an integral number.
    • endString: Specifies the value of endString is set by the function to the next character in the 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 values 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.

Examples of wcstoul() Function

Example 1

The below program illustrates wcstoul() function with base equal to 36.

C




// C program to illustrate
// wcstoul() function
// with base equal to 36
 
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
 
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);
    wprintf(L"String value given is -> %ls\n", string);
 
    wprintf(L"Unsigned Long Int value will be -> %lu\n",
            value);
 
    wprintf(L"End String will be -> %ls\n", endString);
 
    return 0;
}


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 -> 

Example 2

The below program illustrates wcstoul() function with different bases.

C




// C program to illustrate
// wcstoul() function
// with different bases
 
#include <stdio.h>
#include <wchar.h>
 
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);
    wprintf(L"String value --> %ls\n", string);
 
    wprintf(L"Long integer value --> %ld\n", value);
 
    wprintf(L"End String = %ls\n", endString);
 
    // print the unsigned long integer value with
    // the end string with base 16
    value = wcstol(string, &endString, 16);
    wprintf(L"String value --> %ls\n", string);
 
    wprintf(L"Long integer value --> %ld\n", value);
 
    wprintf(L"End String = %ls\n", endString);
 
    // print the unsigned long integer value with
    // the end string with base 12
    value = wcstol(string, &endString, 12);
    wprintf(L"String value --> %ls\n", string);
 
    wprintf(L"Long integer value --> %ld\n", value);
 
    wprintf(L"End String = %ls\n", endString);
 
    return 0;
}


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 --> 9999999...


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads