代码之家  ›  专栏  ›  技术社区  ›  rubenvb

宽图多字节问题

  •  1
  • rubenvb  · 技术社区  · 15 年前

    previous question ,如果我这样做,效果很好:

    wstring temp;
    wcin >> temp;
    
    string whatever( toUTF8(getSomeWString()) );
    
    // store whatever, copy, but do not use it as UTF8 (see below)
    
    wcout << toUTF16(whatever) << endl;
    

    àçé 作为输入,并添加 cout << whatever 声明,我去拿 …‡‚ 作为输出。

    我还可以使用这个字符串与其他字符串进行比较吗,这些字符串是从ASCII源代码获得的?或者换个角度问:我是否会输出 通过linux中的UTF8 cout,它会读吗 § ? 是字符串的字节内容 § ,在utf8linux中由cin读取,与win32api得到的完全相同?

    PS:我之所以这么问是因为我需要大量使用字符串来与其他读取值进行比较(比较和连接…)。

    2 回复  |  直到 9 年前
        1
  •  5
  •   Martin Ba    15 年前

    首先我要说的是 cout (假设您使用visualstudio编译)。 但是,您可以为测试做的是通过win32api fn输出UTF-8文本 WriteConsoleA

    if(!SetConsoleOutputCP(CP_UTF8)) { // 65001
        cerr << "Failed to set console output mode!\n";
        return 1;
    }
    HANDLE const consout = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD nNumberOfCharsWritten;
    const char* utf8 = "Umlaut AE = \xC3\x84 / ue = \xC3\xBC \n";
    if(!WriteConsoleA(consout, utf8, strlen(utf8), &nNumberOfCharsWritten, NULL)) {
        DWORD const err = GetLastError();
        cerr << "WriteConsole failed with << " << err << "!\n";
        return 1;
    }
    

    Umlaut AE = Ä / ue = ü 如果您将控制台(cmd.exe)设置为使用Lucida控制台字体。

    win23 API转换的字符串是

    我会说是的:给定一个Unicode字符序列,它是通过 WideCharToMultiByte

        2
  •  1
  •   James    15 年前

    我怀疑linux中的UTF8 cout是否会产生相同的正确输出,除非它是常规的ASCII值,如UTF8 UTF-8 encoding forms are binary-compatible with ASCII for code points below 128, 我假设UTF16以一种简单的方式出现在UTF8之后。

    好消息是有很多 converters

    推荐文章