The wcstoimax and wcstoumax function in C/C++ operate in the same way as the strtoimax and strtoumax functions in C++, other than they are used to convert the data of a wide string (wstring) to an integral number of the given base. This function is defined in the header file cinttypes .
Header file:
#include <inttypes.h>
wcstoimax method:
wcstoimax is an abbreviation for "Wide Character String to Maximum Integer" . It transforms a wide-character string to a numerical maximum-width signed integer of type intmax_t . It can convert various bases (e.g., decimal, hexadecimal, octal) according to the format of the input text.
Syntax:
It has the following syntax:
#include <inttypes.h>
intmax_t wcstoimax(const wchar_t *npt, wchar_t **endpt, int bases);
Parameters:
npt: The number to be translated into a wide-character string.
endpt: A pointer to wide characters containing the address that represents the function's first incorrect character in the given input string.
bases: The number system's base (for example, 10 for decimal, 16 for hexadecimal, and so on).
Return Value:
The function returns two values, which are as follows:
- If there is a proper conversion, the function returns the resultant integral number as an integer value.
- If no proper conversion can be done, a value of zero (0) is returned.
Example:
// Program to implement the
// wstoimax() function in C++
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
wstring st = L"Programming";
intmax_t value = wcstoimax(st.c_str(), nullptr, 36);
wcout << st << " in base 36 is " << value << " in base 10\n\n";
wchar_t* endp;
value = wcstoimax(st.c_str(), &endp, 30);
// Because 'w' is not in base 30, string
// above this cannot be transformed.
wcout << "The given input String is = " << st << endl;
wcout << "The Number with the base 30 in the given string " << value << " in base 10" << endl;
//statement for the display of the next string
wcout << "the end of the string points to " << endp << endl;
return 0;
}
Output:
Programming in base 36 is 94215099711813100 in base 10
The given input String is = Programming
The Number with the base 30 in the given string 15309807264430906 in base 10
the end of the string points to
wcstoumax method:
The wcstoumax method transforms the wide-character string npt to the integer type uintmaxt . Base input values are 0 and in the range of 2-36. The wcstoumax function is the same as the wcstoul and wcstoull functions. The only distinction is that the returned value corresponds to the type uintmaxt.
Syntax:
It has the following syntax:
#include <inttypes.h>
uintmax_t wcstoumax(const wchar_t *npt, wchar_t **endpt, int bases);
Parameters:
npt: The number to be translated into a wide-character string.
endpt: It is a pointer that contains wide characters containing the address that represents the function's first incorrect character in the given input string.
bases: The number system's base (for example, 10 for decimal, 16 for hexadecimal, and so on).
Example:
// Program to implement the
// wcstoumax() function in C++
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int baseValue = 10;
// L is the literal value to represent the wide string
wstring st = L"123abc";
wchar_t* endp;
uintmax_t number;
number = wcstoumax(st.c_str(), &endp, baseValue);
wcout << "The given input String = " << st << endl;
wcout << "The Value stored in the number is: " << number<< endl;
if (*endp) {
wcout << "The end of the string points to " << endp << endl
<< endl;
}
else {
wcout << "Null pointer" << endl
<< endl;
}
//Because there is no number character in this case, the method returns 0.
baseValue = 10;
wstring st2 = L"abcdeed";
number = wcstoumax(st2.c_str(), &endp, baseValue);
wcout << "The given input String = " << st2 << endl;
wcout << "The Value stored in the number is: " << number<< endl;
if (*endp) {
wcout << "The end of the string points to " << endp << endl
<< endl;
}
else {
wcout << "Null pointer" << endl
<< endl;
}
return 0;
}
Output:
The given input String = 123abc
The Value stored in the number is: 123
The end of the string points to abc
The given input String = abcdeed
The Value stored in the number is: 0
The end of the string points to abcdeed
Uses of wcstoimax and wcstoumax function:
- When dealing with maximum-width signed integers in multiple bases, use wcstoimax to convert wide-character sequences representing numbers to intmax_t type.
- When dealing with maximum-width unsigned integers in multiple bases, use wcstoumax to convert wide-character strings denoting integers to uintmax_t type.