【问题标题】:Output unicode symbol π and ≈ in c++ win32 console application在 c++ win32 控制台应用程序中输出 unicode 符号 π 和 ≈
【发布时间】:2013-01-17 09:49:48
【问题描述】:

我对编程相当陌生,但似乎π(pi) 符号不在ASCII 处理的标准输出集中。

我想知道是否有办法让控制台输出π 符号,从而表达有关某些数学公式的准确答案。

【问题讨论】:

  • 你的控制台字体是什么?只需使用 CMD.EXE 检查。它因 Windows 版本而异,可以自定义。

标签: c++ winapi unicode ascii pi


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    Microsoft CRT 不是很熟悉 Unicode,因此可能需要绕过它并直接使用 WriteConsole()。我假设您已经为 Unicode 编译,否则您需要明确使用 WriteConsoleW()

    【讨论】:

      【解决方案3】:

      我正处于学习阶段,如果我有错误,请纠正我。

      这似乎是一个三步过程:

      1. 使用宽版本的 cout、cin、string 等。所以: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;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-10
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-04
        相关资源
        最近更新 更多