c++ - Windows CMD 无法正确输出 UTF-16

标签 c++ unicode encoding utf-8 utf-16

<分区>

我正在尝试将非 ascii 字符输出到 Windows CMD,但问题是它不起作用。我没有写下面的代码,我把两部分粘在一起了。该代码应该将字符转换为 UTF-8,然后从 UTF-8 转换为 UTF-16,以便它可以在 Windows 上正确显示。这是代码:

// codecvt::in example
#include <iostream>       // std::wcout, std::wcout
#include <locale>         // std::locale, std::codecvt, std::use_facet
#include <string>         // std::wstring
#include <cwchar>         // std::mbstate_t

void GetUnicodeChar(unsigned int code, char chars[5]) {
        if (code <= 0x7F) {
            chars[0] = (code & 0x7F); chars[1] = '\0';
        } else if (code <= 0x7FF) {
            // one continuation byte
            chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[0] = 0xC0 | (code & 0x1F); chars[2] = '\0';
        } else if (code <= 0xFFFF) {
            // two continuation bytes
            chars[2] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[0] = 0xE0 | (code & 0xF); chars[3] = '\0';
        } else if (code <= 0x10FFFF) {
            // three continuation bytes
            chars[3] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[2] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
            chars[0] = 0xF0 | (code & 0x7); chars[4] = '\0';
        } else {
            // unicode replacement character
            chars[2] = 0xEF; chars[1] = 0xBF; chars[0] = 0xBD;
            chars[3] = '\0';
        }
    }

int main ()
{
  typedef std::codecvt<wchar_t,char,std::mbstate_t> facet_type;

  std::locale mylocale;

  const facet_type& myfacet = std::use_facet<facet_type>(mylocale);

  char mystr[5];
  GetUnicodeChar(225, mystr);

  // prepare objects to be filled by codecvt::in :
  wchar_t pwstr[sizeof(mystr)];              // the destination buffer (might be too short)
  std::mbstate_t mystate = std::mbstate_t(); // the shift state object
  const char* pc;                            // from_next
  wchar_t* pwc;                              // to_next

  // translate characters:
  facet_type::result myresult = myfacet.in (mystate,
      mystr, mystr+sizeof(mystr), pc,
      pwstr, pwstr+sizeof(mystr), pwc);

  if ( myresult == facet_type::ok )
  {
    std::wcout << L"Translation successful: ";
    std::wcout << pwstr << std::endl;
  }
  return 0;
}

问题是,当我将数字 225(unicode 字符 á 的十进制表示)提供给 GetUnicodeChar 函数时,输出在 OSX 上是正确的,因为它显示字母 á 但在 Windows 上它显示字符 ├í。但我认为 Windows 在内部使用 UTF-16,这就是我认为这应该有效的原因。但事实并非如此。

最佳答案

您需要先设置 _O_U16TEXT 模式:

_setmode(_fileno(stdout), _O_U16TEXT);

有关旧 Michael Kaplain 博客条目的更多信息:http://www.siao2.com/2008/03/18/8306597.aspx

关于c++ - Windows CMD 无法正确输出 UTF-16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23479657/

相关文章:

C++ 最佳实践 - 函数类型别名 std::function<T> 或 T

c++ - 使用 `Command terminated` 时为 `cin`

python - 如何在 python 3.6 中打开混合编码的 unicode 文件?

android - 在 Android Chrome 浏览器的移动 Web 应用程序菜单中未检测到 HTML unicode ☰

java - 处理带有变音符号和其他特殊字符的 URL

c++ - 关于 SPOJ 测试的查询

c++ - 内存处理问题

python - 为什么Python 2.x使用字符串格式化+unicode会抛出异常?

用于上传的 SIlverlight 编码 PCM

iPhone - 将 NSString 编码从 WindowsCP1251 转换为 UTF8