【问题标题】:how to send international characters to windows console?如何将国际字符发送到 Windows 控制台?
【发布时间】:2012-06-09 15:15:48
【问题描述】:

代码:

#include <windows.h>

int main() {
  SetConsoleOutputCP(CP_UTF8);
  system("echo Ιλιάδα");
}

在控制台上打印:Ιλιάδα 源代码采用 UTF-8 编码,带有 BOM。

但如果我尝试:system(L"echo Ιλιάδα");,我会得到错误:error: cannot convert 'const wchar_t*' to 'const char*' for argument '1' to 'int system(const char*)'。当然,我没想到这里还有其他东西。有没有其他函数可以接受这些字符?

【问题讨论】:

    标签: c++ windows utf-8 console mingw


    【解决方案1】:

    _wsystem 用于宽字符串。

    【讨论】:

      【解决方案2】:

      这是 VC++ 吗?

      如果是这样,您的文件似乎使用了没有 BOM 的 UTF-8,这意味着 VC++ 将假定源字符集和执行字符集相同,因此在生成字符串文字“echo”时不会进行任何编码转换Ιλιάδα”。它只会直接输出 UTF-8 数据。这意味着编译器认为您编写了 system("echo Ιλιάδα");,其中垃圾是您的 UTF-8 字符串,被视为系统的语言环境编码。

      默认情况下,system() 函数采用系统语言环境编码的字符串。 控制台输出代码页对system()的操作没有影响,所以上面的字符串也正是它所看到的。


      因为您使用的是没有 BOM 的 UTF-8,所以您会遇到宽字符串的问题。生成宽字符串需要将源字符集正确转换为宽执行字符集。如果您使用没有 BOM 的 UTF-8,则 VC++ 不知道正确的源编码,因此无法正确进行此转换。

      【讨论】:

      • 我的编码是用BOM,我用的是MinGW。
      【解决方案3】:

      您可以尝试 system("cmd /c lambdabath") 或 system("lambdabath") 示例:

      //Save As UTF-8 withput BOM signature
      
      #include <stdlib.h>
      #include <windows.h>
      
      int main() {
          SetConsoleOutputCP(CP_UTF8);
          //system("cmd /c lambdabath");
          system("lambdabath");
      }
      

      lambdabath.bat(也保存为没有 BOM 签名的 UTF-8):

      chcp 65001
      echo Ιλιάδα
      

      但如果问题是:如何将国际字符发送到 Windows 控制台?
      然后你可以尝试不同的编码。不需要使用函数 system()。

      Windows 1253 编码:

      //Save As Windows 1253
      
       #include <stdio.h>
       #include <windows.h>
      
      int main()
      {
          SetConsoleOutputCP(1253);
          char *ansichar_ = "Ιλιάδα";
          unsigned char lambda1253char = 'λ';
          printf("ansichar_: %s\n", ansichar_);
          printf("λ %#x\n", lambda1253char);
      }
      

      结果:

      ansichar_: Ιλιάδα
      λ 0xeb
      

      UTF-16 编码:

       //Save As UTF16 (Unicode)
      
          #include <stdio.h>
          #include <io.h>
          #include <fcntl.h>
          #include <wchar.h>
      
          int main()
          {
              _setmode(_fileno(stdout), _O_U16TEXT);
              wchar_t *wchar_ = L"Ιλιάδα";
              wchar_t lambdaWchar = L'λ';
              wprintf(L"wchar_: %s\n", wchar_);
              wprintf(L"λ %#x\n", lambdaWchar);
          }
      

      结果:

      wchar_: Ιλιάδα
      λ 0x3bb
      

      UTF-8 编码:

       //Save As UTF8 without BOM signature
      
          #include <stdio.h>
          #include <windows.h>
      
          int main()
          {
              SetConsoleOutputCP(65001);
              char *utf8char_ = "Ιλιάδα";
              int lambdaUTF8char = 'λ';
              printf("utf8char_: %s\n", utf8char_);
              printf("λ %#x\n", lambdaUTF8char);
          }
      

      结果:

      utf8char_: Ιλιάδα
      λ 0xcebb
      

      无论如何,设置默认的控制台字体:Lucida Console。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-22
        • 2012-01-26
        • 1970-01-01
        • 2014-03-09
        • 2011-08-18
        • 2013-06-19
        • 2019-07-22
        • 1970-01-01
        相关资源
        最近更新 更多