| 1 |
#include <stdio.h> |
| 2 |
#include <fcntl.h> |
| 3 |
#include <windows.h> |
| 4 |
#include <locale.h> |
| 5 |
|
| 6 |
/* |
| 7 |
|
| 8 |
This program proves that when you set the locale to "", the Chinese ANSI CP936 |
| 9 |
encoded text is printed wrongly in a simplified Chinese regional setting. |
| 10 |
UTF-8 is also printed wrongly. |
| 11 |
|
| 12 |
To test this program you first need to change the Windows locale setting to |
| 13 |
simplified Chinese. There is no problem doing that, because the "display |
| 14 |
language" will stay the original language, and you can still use your Windows. |
| 15 |
|
| 16 |
Control Panel > Region and Language > Administrative > Change system locale |
| 17 |
|
| 18 |
Select simplified Chinese and reboot your PC. |
| 19 |
|
| 20 |
For example output see setlocale.png. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
print_string(const char* str, const char* ustr, const wchar_t *wstr) { |
| 25 |
int prevmode; |
| 26 |
UINT outputCP; |
| 27 |
int utf8 = 0; |
| 28 |
|
| 29 |
/* Set utf8 to 1 to print UTF-8 text. |
| 30 |
If you print both UTF-16 en UTF-8 in one program the console gets |
| 31 |
mixed up. When the UTF-8 is printed after UTF-16 the UTF-16 text |
| 32 |
is displayed wrongly. |
| 33 |
*/ |
| 34 |
|
| 35 |
if ( ! utf8 ) { |
| 36 |
/* When the locale is set to "" the following line will produce wrong output */ |
| 37 |
printf ("ANSI CP936 %s\n",str); |
| 38 |
|
| 39 |
/* UTF-16 will produce correct output in all cases. */ |
| 40 |
prevmode = _setmode(_fileno(stdout), _O_U16TEXT); |
| 41 |
wprintf(L"UTF-16 %ls\n",wstr); |
| 42 |
_setmode(_fileno(stdout), prevmode); |
| 43 |
} else { |
| 44 |
|
| 45 |
/* UTF-8 will produce wrong output when the locale is "". |
| 46 |
When the locale is "C" wrong output with raster font, and correct output |
| 47 |
with TrueType font. */ |
| 48 |
outputCP = GetConsoleOutputCP(); |
| 49 |
SetConsoleOutputCP(CP_UTF8); |
| 50 |
wprintf(L"UTF-8 %S\n",ustr); |
| 51 |
SetConsoleOutputCP(outputCP); |
| 52 |
|
| 53 |
/* The code below produces wrong output in all cases */ |
| 54 |
// prevmode = _setmode(_fileno(stdout), _O_U8TEXT); |
| 55 |
// wprintf(L"UTF-8 %S",ustr); |
| 56 |
// _setmode(_fileno(stdout), prevmode); |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
int main() { |
| 62 |
|
| 63 |
char str[5]; /* ANSI CP936 */ |
| 64 |
char ustr[15]; /* UTF-8 */ |
| 65 |
wchar_t wstr[10]; /* UTF-16 */ |
| 66 |
|
| 67 |
/* Create ANSI CP936 string (meaning: Western-European). */ |
| 68 |
str[0] = 0xce; |
| 69 |
str[1] = 0xf7; |
| 70 |
str[2] = 0xc5; |
| 71 |
str[3] = 0xb7; |
| 72 |
str[4] = '\0'; |
| 73 |
/* Convert CP936 to UTF-16. */ |
| 74 |
MultiByteToWideChar(936, 0, str, -1, wstr, sizeof(wstr)); |
| 75 |
/* Convert UTF-16 to UTF-8 */ |
| 76 |
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, ustr, sizeof(ustr), NULL, NULL); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
setlocale (LC_ALL, ""); |
| 81 |
printf("==> setlocale (LC_ALL, \"\");\n"); |
| 82 |
print_string(str, ustr, wstr); |
| 83 |
|
| 84 |
setlocale (LC_ALL, "C"); |
| 85 |
printf("\n==> setlocale (LC_ALL, \"C\");\n"); |
| 86 |
print_string(str, ustr, wstr); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |