3 回答
TA貢獻1744條經驗 獲得超4個贊
_setmode(..., _O_U16TEXT).
解決辦法:
#include <iostream>#include <io.h>#include <fcntl.h>int wmain(int argc, wchar_t* argv[]){
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"Testing unicode -- English -- Ελληνικ? -- Espa?ol." << std::endl;}TA貢獻1841條經驗 獲得超3個贊
中文Unicode Hello World
提綱
Unicode項目設置 將控制臺代碼頁設置為Unicode 查找并使用支持要顯示的字符的字體。 使用要顯示的語言的區域設置。 使用寬字符輸出,即 std::wcout
1項目設置
_UNICODEUNICODE
int wmain(int argc, wchar_t* argv[])
wmainmainwmain
2.控制臺代碼頁
chcpCP_UTF8
SetConsoleOutputCP(CP_UTF8);SetConsoleCP(CP_UTF8);
3.選擇字體
CONSOLE_FONT_INFOEX fontInfo;// ... configure fontInfoSetCurrentConsoleFontEx(hConsole, false, &fontInfo);
4.設置地區
char* a = setlocale(LC_ALL, "chinese");
chinesegerman
5.使用寬字符輸出
std::wcout << L"你好" << std::endl;
LUCS-2 LE BOM.
例
#include <Windows.h>#include <iostream>#include <io.h>#include <fcntl.h>#include <locale.h>#include <wincon.h>int wmain
(int argc, wchar_t* argv[]){
SetConsoleTitle(L"My Console Window - 你好");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
char* a = setlocale(LC_ALL, "chinese");
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
CONSOLE_FONT_INFOEX fontInfo;
fontInfo.cbSize = sizeof(fontInfo);
fontInfo.FontFamily = 54;
fontInfo.FontWeight = 400;
fontInfo.nFont = 0;
const wchar_t myFont[] = L"KaiTi";
fontInfo.dwFontSize = { 18, 41 };
std::copy(myFont, myFont + (sizeof(myFont) / sizeof(wchar_t)), fontInfo.FaceName);
SetCurrentConsoleFontEx(hConsole, false, &fontInfo);
std::wcout << L"Hello World!" << std::endl;
std::wcout << L"你好!" << std::endl;
return 0;}TA貢獻1802條經驗 獲得超4個贊
int _tmain(int argc, _TCHAR* argv[]){
char* locale = setlocale(LC_ALL, "English"); // Get the CRT's current locale.
std::locale lollocale(locale);
setlocale(LC_ALL, locale); // Restore the CRT.
std::wcout.imbue(lollocale); // Now set the std::wcout to have the locale that we got from the CRT.
std::wcout << L"?Hola!";
std::cin.get();
return 0;}- 3 回答
- 0 關注
- 1573 瀏覽
添加回答
舉報
