wcstoimax() and wcstoumax() function in C++
Wcstoimax() and wcstoumax() are functions used in C and C++ programming that convert wide-character strings to integer values. These functions are included in the C++ and C language in the <cinttypes> and <inttypes.h> headers. The goal of these functions is to translate wide-character strings that represent numbers into the appropriate integer types (uintmax_t and intmax_t).
What is the Wcstoimax() function?
The wcstoimax() is a method for transforming wide-character strings to integer values. An unsigned integer of type uintmax_t is converted from a wide-character string using the wcstoimax() function.
Syntax:
It has the following syntax:
#include <inttypes.h>
intmax_t wcstoimax(const wchar_t *nptr, wchar_t **endptr, int base);
Parameters:
- nptr: It is a pointer to the wide-character string that holds the integer's representation.
- endptr: It is a pointer to a pointer to wchar_t, the address of the first invalid wide-character that is stored by the function.
- Base: The decimal system's base, such as 10 in the case of decimals.
Return Value:
- It returns the converted integer value of type intmax_t.
- The function returns the converted integral number as an integer value if the conversion is valid.
- A zero value is returned (0) if no valid conversion could be carried out at that time.
- The wide-character string that nptr points to has its beginning portion converted to an integer value of type intmax_t by the wcstoimax() function. The first character that’s not a valid component of the number in the specified base is where the conversion terminates.
What is the Wcstoumax() function?
The wcstoumax() is a method for transforming wide-character strings to integer values. A signed integer of type intmax_t is converted from a wide-character string using the wcstoumax().
Syntax:
It has the following syntax:
#include <inttypes.h>
uintmax_t wcstoumax(const wchar_t *nptr, wchar_t **endptr, int base);
Parameters of Syntax:
- nptr: It is a pointer to the wide-character string that holds the integer's representation.
- endptr: It is a pointer to a pointer to wchar_t, the address of the first invalid wide-character that is stored by the function.
- Base: The decimal system's base, such as 10 in the case of decimals.
Return Value:
- It returns the converted integer value of type uintmax_t.
- The function returns the converted integral number as an integer value if the conversion is valid.
- A zero value is returned (0) if no valid conversion could be carried out at that time.
- Similar to wcstoimax(), wcstoumax() yields an unsigned integer of type uintmax_t.
Example:
Let us take an example to illustrate the use of wcstoimax() function in C++.
#include <iostream>
#include <cinttypes>
int main() {
// Example wide-character strings
const wchar_t* intStr = L"12345";
const wchar_t* uintStr = L"67890";
// Convert wide-character string to intmax_t
wchar_t* intEndPtr;
intmax_t intValue = wcstoimax(intStr, &intEndPtr, 10);
if (*intEndPtr == L'\0') {
std::wcout << L"Converted intmax_t value: " << intValue << std::endl;
} else {
std::wcout << L"Conversion to intmax_t failed at character: " << *intEndPtr << std::endl;
}
// Convert wide-character string to uintmax_t
wchar_t* uintEndPtr;
uintmax_t uintValue = wcstoumax(uintStr, &uintEndPtr, 10);
if (*uintEndPtr == L'\0') {
std::wcout << L"Converted uintmax_t value: " << uintValue << std::endl;
} else {
std::wcout << L"Conversion to uintmax_t failed at character: " << *uintEndPtr << std::endl;
}
return 0;
}
Output:
Converted intmax_t value: 98852
Converted uintmax_t value: 67890
Example 2:
Let us take another example to illustrate the use of wcstoimax() function in C++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
wstring str = L"Tutorialandexample";
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);
// 'w' value does not lies in base 30 so string
// beyond this cannot be converted
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:
Tutorialandexample in base 36 is 9223372036854775807 in base 10
Given String = Tutorialandexample
Number with base 30 in string 29 in base 10
End String points to utorialandexample
Example 3:
Let us take an example to illustrate the use of wcstoumax() function in C++.
#include <iostream>
#include <cinttypes>
int main()
{
int base = 16; // Change the base to your desired value
std::wstring str = L"1a2b3c"; // Change the string to your desired input
wchar_t *end;
uintmax_t num;
num = wcstoumax(str.c_str(), &end, base);
std::wcout << L"Given String = " << str << std::endl;
std::wcout << L"Value stored in num " << num << std::endl;
if (*end)
{
std::wcout << L"End string points to " << end << std::endl << std::endl;
}
else
{
std::wcout << L"Null pointer" << std::endl<< std::endl;
}
// Change the base and string for a different test
base = 10;
std::wstring str2 = L"9876";
std::wcout << L"Given String = " << str2 << std::endl;
// Convert wide-character string to uintmax_t with a different base
num = wcstoumax(str2.c_str(), &end, base);
// Display the result and information about the conversion
std::wcout << L"Number with base " << base << L" in string " << num << std::endl;
if (*end)
{
std::wcout << L"End String points to " << end;
}
else
{
std::wcout << L"Null pointer";
}
return 0;
}
Outout:
Given String = 1a2b3c
Value stored in num 1715004
Null pointer
Given String = 9876
Number with base 10 in string 9876
Null pointer
Conclusion:
Strong and adaptable input processing is made possible by the extensive mechanism offered by the C and C++ wcstoimax() and wcstoumax() methods for transforming wide-character strings to integer values. Providing standardized functionality for handling conversions, these methods are included in the <inttypes.h> header in C and <cinttypes> in C++. An unsigned integer of type uintmax_t is converted from a wide-character string using the wcstoimax() function, and a signed integer of type intmax_t is obtained using wcstoumax(). A pointer to handle the conversion's end, the desired base of the numeral system, and arguments for the wide-character string are required by both functions. In addition to giving the end pointer information on the conversion's success, this enables exact control over the process.