+ 2
Wide characters to character
Hello how one can convert wide character to character and vice versa in c++?
1 Answer
+ 3
Hi, i think you need the wcstombs:
Convert wide-character string to multibyte string
Translates wide characters from the sequence pointed by src to the multibyte equivalent sequence (which is stored at the array pointed by dest), up until either max bytes have been translated or until a wide characters translates into a null character.
http://www.cplusplus.com/reference/cstdlib/wcstombs/
You are looking for wctomb(): it's in the ANSI standard, so you can count on it. It works even when the wchar_t uses a code above 255. You almost certainly do not want to use it.
wchar_t is an integral type, so your compiler won't complain if you actually do:
char x = (char)wc;
but because it's an integral type, there's absolutely no reason to do this. If you accidentally read Herbert Schildt's C: The Complete Reference, or any C book based on it, then you're completely and grossly misinformed. Characters should be of type int or better. That means you should be writing this:
int x = getchar();
and not this:
char x = getchar(); /* <- WRONG! */
https://stackoverflow.com/questions/3019977/convert-wchar-t-to-char