2013-01-17 64 views

回答

0

Microsoft CRT不是非常精通Unicode的,因此可能需要繞過它並直接使用WriteConsole()。我假設你已經編譯爲Unicode,否則你需要明確使用WriteConsoleW()

2

我不太確定任何其他方法(如使用STL的方法),但可以使用WriteConsoleW在Win32上執行此操作:

HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
LPCWSTR lpPiString = L"\u03C0"; 

DWORD dwNumberOfCharsWritten; 
WriteConsoleW(hConsoleOutput, lpPiString, 1, &dwNumberOfCharsWritten, NULL); 
0

我在這個學習階段,所以糾正我,如果我得到錯誤的東西。

看起來這是一個三個步驟的過程:

  1. 使用COUT,CIN,字符串的寬版等。所以:wcout,wcin,wstring
  2. 在使用流之前,將它設置爲Unicode友好模式。
  3. 將目標控制檯配置爲使用支持Unicode的字體。

你現在應該能夠搖滾那些時髦的åäös。

例子:

#include <iostream> 
#include <string> 
#include <io.h> 

// We only need one mode definition in this example, but it and several other 
// reside in the header file fcntl.h. 

#define _O_WTEXT  0x10000 /* file mode is UTF16 (translated) */ 
// Possibly useful if we want UTF-8 
//#define _O_U8TEXT  0x40000 /* file mode is UTF8 no BOM (translated) */ 

void main(void) 
{ 
    // To be able to write UFT-16 to stdout. 
    _setmode(_fileno(stdout), _O_WTEXT); 
    // To be able to read UTF-16 from stdin. 
    _setmode(_fileno(stdin), _O_WTEXT); 

    wchar_t* hallå = L"Hallå, värld!"; 

    std::wcout << hallå << std::endl; 

     // It's all Greek to me. Go UU! 
    std::wstring etabetapi = L"η β π"; 

    std::wcout << etabetapi << std::endl; 

    std::wstring myInput; 

    std::wcin >> myInput; 

    std:: wcout << myInput << L" has " << myInput.length() << L" characters." << std::endl; 

    // This character won't show using Consolas or Lucida Console 
    std::wcout << L"♔" << std::endl; 
} 
相關問題