Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Apr 30, 2001



How to Help Your Business Become an AI Early Adopter

Converting wchar_t to char

In addition to the widen() member function, the iostream framework supports the narrow() member function which performs the reverse operation, i.e., converting a wide character to its corresponding char value. The narrow() member function has the following prototype:
char narrow(char_type c, char deflt) const;

The first argument is the wide-char value that is to be converted to char. If the conversion is possible, narrow() returns the corresponding value of c. Otherwise, it returns deflt. Remember that not every wide-char character can be converted to char; wide-char values higher than 255 cannot be represented as char. Therefore, in these cases, narrow returns deflt.

In the following example, the user converts the wide-char letter L'a' (an uppercase L indicates a wchar_t character) to its char equivalent:
char c=cin.narrow(L'a', 0); // return 0 if conversion fails
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date