The wcstol() function in C/C++ converts the given wide string to a 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:
long int wcstol( const wchar_t* str, wchar_t** endString, int base )
Parameters: The function accepts three mandatory parameters which are described below:
- string : String beginning with an integral form.
- endString : This is set by the function to the next character in string after the last valid character.
- base : 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 a long int value.
- Returns zero, if no valid conversion could be performed.
- If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN (defined in ), and errno is set to ERANGE.
Below programs illustrate the above function:
Program 1 :
// C++ program to illustrate // wcstol() function #include <bits/stdc++.h> using namespace std;
int main()
{ // initialize the string
wchar_t string[] = L "101GeeksForGeeks" ;
// initialize the base
int base = 3;
wchar_t * endString;
// print the long integer value with the end string
long value = wcstol(string, &endString, base);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
return 0;
} |
String value --> 101GeeksForGeeks Long integer value --> 10 End String = GeeksForGeeks
Program With different bases :
Program 2 :
// C++ program to illustrate // wcstol() function // with different bases #include <bits/stdc++.h> using namespace std;
int main()
{ // initialize the string
wchar_t string[] = L "101GFG" ;
// initialize the base
wchar_t * endString;
// print the long integer value with the end string
// with base 2
long value = wcstol(string, &endString, 2);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
// print the long integer value with the end string
// with base 5
value = wcstol(string, &endString, 5);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
// print the 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 --> 101GFG Long integer value --> 5 End String = GFG String value --> 101GFG Long integer value --> 26 End String = GFG String value --> 101GFG Long integer value --> 145 End String = GFG