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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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