wcstoimax() and wcstoumax() function in C/C++
Last Updated :
11 Sep, 2018
The wcstoimax() and wcstoumax() function in C/C++ works exactly same as strtoimax() and strtoumax() function in C++ but are used to convert the contents of a wide string (wstring) as an integral number of the specified base. This function is defined in cinttypes header file.
Syntax:
uintmax_t wcstoumax(const wchar* wstr, wchar** end, int base);
intmax_t wcstoimax(const wchar* wstr, wchar** end, int base);
Parameters: The function accepts three mandatory parameters as described below:
- wstr: specifies a wide string consist of an integral number.
- end: specifies a reference to object of type wchar*. The value of end is set by the function to the next character in wstr after the last valid numeric character. This parameter can also be a null pointer, in case if it is not used.
- base: specifies the numerical base (radix) that determines the valid characters and their interpretation in the string
Return Type: The function returns two values as follows:
- If valid conversion occur then the function returns the converted integral number as integer value.
- If no valid conversion could be performed then, a zero value is returned (0)
Below programs illustrate the above function:
Program 1 :
#include <bits/stdc++.h>
using namespace std;
int main()
{
wstring str = L "geeksforgeeks" ;
intmax_t val = wcstoimax(str.c_str(), nullptr, 36);
wcout << str << " in base 36 is " << val << " in base 10\n\n" ;
wchar_t * end;
val = wcstoimax(str.c_str(), &end, 30);
wcout << "Given String = " << str << endl;
wcout << "Number with base 30 in string " << val << " in base 10" << endl;
wcout << "End String points to " << end << endl;
return 0;
}
|
Output:
geeksforgeeks in base 36 is 9223372036854775807 in base 10
Given String = geeksforgeeks
Number with base 30 in string 8759741037015451228 in base 10
End String points to
Program 2 :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int base = 10;
wstring str = L "12345abcd" ;
wchar_t * end;
uintmax_t num;
num = wcstoumax(str.c_str(), &end, base);
wcout << "Given String = " << str << endl;
wcout << "Value stored in num " << num << endl;
if (*end) {
wcout << "End string points to " << end << endl
<< endl;
}
else {
wcout << "Null pointer" << endl
<< endl;
}
base = 10;
wstring str2 = L "abcd" ;
wcout << "Given String = " << str2 << endl;
num = wcstoumax(str2.c_str(), &end, base);
wcout << "Number with base 10 in string " << num << endl;
if (*end) {
wcout << "End String points to " << end;
}
else {
wcout << "Null pointer" ;
}
return 0;
}
|
Output:
Given String = 12345abcd
Value stored in num 12345
End string points to abcd
Given String = abcd
Number with base 10 in string 0
End String points to abcd
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...