Wcstoimax And Wcstoumax Function In Cc++ - C++ Programming Tutorial
C++ Course / Functions / Wcstoimax And Wcstoumax Function In Cc++

Wcstoimax And Wcstoumax Function In Cc++

BLUF: Mastering Wcstoimax And Wcstoumax Function In Cc++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Wcstoimax And Wcstoumax Function In Cc++

C++ is renowned for its efficiency. Learn how Wcstoimax And Wcstoumax Function In Cc++ enables low-level control and high-performance computing in the tutorial below.

The wcstoimax and wcstoumax functions in C/C++ function similarly to the strtoimax and strtoumax functions in C++, with the distinction that they specifically handle the conversion of wide strings (wstring) to integral numbers in a specified base. These functions are declared in the cinttypes header file.

Header file:

Example

#include <inttypes.h>

wcstoimax method:

wcstoimax stands for "Wide Character String to Maximum Integer" and is used to convert a wide-character string into a signed integer of type intmax_t with the maximum width. This function supports conversion of wide-character strings in different bases such as decimal, hexadecimal, and octal, based on the format of the provided input text.

Syntax:

It has the following syntax:

Example

#include <inttypes.h>
intmax_t wcstoimax(const wchar_t *npt, wchar_t **endpt, int bases);

Parameters:

The npt parameter represents the numerical value that needs to be converted into a wide-character string.

endpt: A reference to wide characters storing the location indicating the initial erroneous character of the function within the provided input string.

The base of a number system refers to the numerical radix used, such as 10 for decimal, 16 for hexadecimal, and other variations.

Return Value:

The function provides dual outputs in the following manner:

  • In cases where a valid conversion exists, the function yields the resulting integer value.
  • Should a valid conversion not be achievable, the function outputs a value of zero (0).
  • Example:

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:

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 function converts the wide-character string npt to the unsigned integer type uintmaxt. The base input ranges from 0 to between 2 and 36. This function behaves similarly to wcstoul and wcstoull, with the key difference being that it outputs a value of type uintmaxt.

Syntax:

It has the following syntax:

Example

#include <inttypes.h>
uintmax_t wcstoumax(const wchar_t *npt, wchar_t **endpt, int bases);

Parameters:

npt: The numerical value to be converted into a wide-character string.

endpt: It is a pointer that holds wide characters indicating the location of the initial incorrect character in the provided input string within the function.

bases: The base of a number system (e.g., 10 for decimal, 16 for hexadecimal, etc.).

Example:

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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience