How one can convert wchar_t to char in C? Please suggest the code.
How one can convert wchar_t to char in C? Please suggest the code.
The following page would seem to do what you want.
Converting wchar_t to char
narrow will, however, lose information on the character. So it really depends on what the wstring contains.
Usually, a normal string contains an ASCII string. If you don't know what ASCII is, look it up.
A wstring character is 2 bytes in Linux and 4 bytes in Windows. And whatever it contains depends on how you use it. To be honest, I think a wide string actually degrades the portability because of this. Because what are you going to put in it? A UTF16 string? A UTF32 string is not portable.
If your wide string is in UTF16 or UTF32, however, you can't simply narrow it. You'll need some way of representing special characters. For this, you can use UTF8 - normally.
I suggest you to read at least the wikipedia entries on unicode and
. They're quite good. And you'll understand what I mean if you don't already. Basically, I'd use UTF8 in memory. So who needs wide strings?Code:UTF{8,16,32}*
*) This is put in a code block because the stupid forum software doesn't allow posting this without it. It thinks it's code. Well, it's not. Stupid software.
Given your question, and given this is C.
Perhaps ask a smarter question.Code:wchar_t x = ...; char y = (char) x;
Code:void convert_to_string(WCHAR *crap, char *buf) { // convert wide-char to normal char while (*buf++ = (char)*crap++); }
ggCode:#include <stdio.h> #include <stdlib.h> #include <locale.h> int main() { // U+0192 : LATIN SMALL LETTER F WITH HOOK const wchar_t f_hook = 0x0192; // this character is 0x85 in CP1252 setlocale(LC_ALL, ".1252"); char c; wctomb(&c, f_hook); printf("%c = 0x%X\n", c, (unsigned char)c); return 0; }//main