2013-01-17 141 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; 
} 
相关问题