75

我正在尝试打印一个俄语“ф”(U+0444 西里尔小写字母 EF)字符,该字符的代码为十进制1092。使用 C++,我怎样才能打印出这个字符?我原以为以下内容会起作用,但是...

int main (){
   wchar_t f = '1060';
   cout << f << endl;
}
4

11 回答 11

72

要表示字符,您可以使用通用字符名称 (UCN)。字符 'ф' 的 Unicode 值是 U+0444,所以在 C++ 中你可以写成 '\u0444' 或 '\U00000444'。此外,如果源代码编码支持此字符,那么您可以直接在源代码中编写它。

// both of these assume that the character can be represented with
// a single char in the execution encoding
char b = '\u0444';
char a = 'ф'; // this line additionally assumes that the source character encoding supports this character

打印这些字符取决于您要打印的内容。如果您要打印到 Unix 终端仿真器,终端仿真器正在使用支持此字符的编码,并且该编码与编译器的执行编码相匹配,那么您可以执行以下操作:

#include <iostream>

int main() {
    std::cout << "Hello, ф or \u0444!\n";
}

该程序要求 'ф' 可以用单个字符表示。在 OS X 和大多数现代 Linux 安装上,这都可以正常工作,因为源代码、执行和控制台编码都是 UTF-8(支持所有 Unicode 字符)。

Windows 的情况更加困难,并且有不同的权衡取舍有不同的可能性。

如果您不需要可移植代码(您将使用 wchar_t,在所有其他平台上确实应该避免使用),最好的方法可能是将输出文件句柄的模式设置为仅采用 UTF-16 数据。

#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Hello, \u0444!\n";
}

可移植代码更加困难。

于 2012-08-18T04:42:52.577 回答
17

编译时-std=c++11,可以简单地

  const char *s  = u8"\u0444";
  cout << s << endl;
于 2012-08-18T16:19:28.787 回答
12

最终,这完全取决于平台。不幸的是,标准 C++ 对 Unicode 的支持很差。对于 GCC,您必须将其设为窄字符串,因为它们使用 UTF-8,而 Windows 需要宽字符串,您必须输出到wcout.

// GCC
std::cout << "ф";
// Windoze
wcout << L"ф";
于 2012-08-18T03:26:49.620 回答
8

如果您使用 Windows(注意,我们使用的是 printf(),而不是 cout):

//Save As UTF8 without signature
#include <stdio.h>
#include<windows.h>
int main (){
    SetConsoleOutputCP(65001); 
    printf("ф\n");
}

不是 Unicode,但可以工作 - 1251 而不是 UTF8:

//Save As Windows 1251
#include <iostream>
#include<windows.h>
using namespace std;
int main (){
    SetConsoleOutputCP(1251); 
    cout << "ф" << endl;
}
于 2013-09-28T18:07:25.887 回答
7

此代码适用于 Linux(C++11、geany、g++ 7.4.0):

#include <iostream>

using namespace std;


int utf8_to_unicode(string utf8_code);
string unicode_to_utf8(int unicode);


int main()
{
    cout << unicode_to_utf8(36) << '\t';
    cout << unicode_to_utf8(162) << '\t';
    cout << unicode_to_utf8(8364) << '\t';
    cout << unicode_to_utf8(128578) << endl;

    cout << unicode_to_utf8(0x24) << '\t';
    cout << unicode_to_utf8(0xa2) << '\t';
    cout << unicode_to_utf8(0x20ac) << '\t';
    cout << unicode_to_utf8(0x1f642) << endl;

    cout << utf8_to_unicode("$") << '\t';
    cout << utf8_to_unicode("¢") << '\t';
    cout << utf8_to_unicode("€") << '\t';
    cout << utf8_to_unicode("") << endl;

    cout << utf8_to_unicode("\x24") << '\t';
    cout << utf8_to_unicode("\xc2\xa2") << '\t';
    cout << utf8_to_unicode("\xe2\x82\xac") << '\t';
    cout << utf8_to_unicode("\xf0\x9f\x99\x82") << endl;

    return 0;
}


int utf8_to_unicode(string utf8_code)
{
    unsigned utf8_size = utf8_code.length();
    int unicode = 0;

    for (unsigned p=0; p<utf8_size; ++p)
    {
        int bit_count = (p? 6: 8 - utf8_size - (utf8_size == 1? 0: 1)),
            shift = (p < utf8_size - 1? (6*(utf8_size - p - 1)): 0);

        for (int k=0; k<bit_count; ++k)
            unicode += ((utf8_code[p] & (1 << k)) << shift);
    }

    return unicode;
}


string unicode_to_utf8(int unicode)
{
    string s;

    if (unicode>=0 and unicode <= 0x7f)  // 7F(16) = 127(10)
    {
        s = static_cast<char>(unicode);

        return s;
    }
    else if (unicode <= 0x7ff)  // 7FF(16) = 2047(10)
    {
        unsigned char c1 = 192, c2 = 128;

        for (int k=0; k<11; ++k)
        {
            if (k < 6)  c2 |= (unicode % 64) & (1 << k);
            else c1 |= (unicode >> 6) & (1 << (k - 6));
        }

        s = c1;    s += c2;

        return s;
    }
    else if (unicode <= 0xffff)  // FFFF(16) = 65535(10)
    {
        unsigned char c1 = 224, c2 = 128, c3 = 128;

        for (int k=0; k<16; ++k)
        {
            if (k < 6)  c3 |= (unicode % 64) & (1 << k);
            else if (k < 12) c2 |= (unicode >> 6) & (1 << (k - 6));
            else c1 |= (unicode >> 12) & (1 << (k - 12));
        }

        s = c1;    s += c2;    s += c3;

        return s;
    }
    else if (unicode <= 0x1fffff)  // 1FFFFF(16) = 2097151(10)
    {
        unsigned char c1 = 240, c2 = 128, c3 = 128, c4 = 128;

        for (int k=0; k<21; ++k)
        {
            if (k < 6)  c4 |= (unicode % 64) & (1 << k);
            else if (k < 12) c3 |= (unicode >> 6) & (1 << (k - 6));
            else if (k < 18) c2 |= (unicode >> 12) & (1 << (k - 12));
            else c1 |= (unicode >> 18) & (1 << (k - 18));
        }

        s = c1;    s += c2;    s += c3;    s += c4;

        return s;
    }
    else if (unicode <= 0x3ffffff)  // 3FFFFFF(16) = 67108863(10)
    {
        ;  // actually, there are no 5-bytes unicodes
    }
    else if (unicode <= 0x7fffffff)  // 7FFFFFFF(16) = 2147483647(10)
    {
        ;  // actually, there are no 6-bytes unicodes
    }
    else  ;  // incorrect unicode (< 0 or > 2147483647)

    return "";
}

更多的:

于 2019-06-25T19:44:02.080 回答
3

'1060'是四个字符,不会在标准下编译。如果您的宽字符与 Unicode 1:1 匹配(检查您的语言环境设置),您应该将字符视为数字。

int main (){
    wchar_t f = 1060;
    wcout << f << endl;
}
于 2012-08-18T03:28:15.963 回答
1

我需要在 UI 中显示字符串并将其保存到 xml 配置文件中。上面指定的格式适用于 c++ 中的字符串,我想补充一下,我们可以通过将“\u”替换为“&#x”并添加一个“;”来获得特殊字符的 xml 兼容字符串 在末尾。

例如:C++:“\u0444”--> XML:"&#x0444;"

于 2019-01-29T13:57:34.533 回答
0

在 Linux 中,我可以这样做:

std::cout << "ф";

我只是从这里复制粘贴的字符,至少我尝试过的随机样本没有失败。

于 2017-01-09T10:59:26.737 回答
0

Linux中的另一种解决方案:

string a = "Ф";
cout << "Ф = \xd0\xa4 = " << hex
     << int(static_cast<unsigned char>(a[0]))
     << int(static_cast<unsigned char>(a[1])) << " (" << a.length() << "B)" << endl;

string b = "√";
cout << "√ = \xe2\x88\x9a = " << hex
     << int(static_cast<unsigned char>(b[0]))
     << int(static_cast<unsigned char>(b[1]))
     << int(static_cast<unsigned char>(b[2])) << " (" << b.length() << "B)" << endl;
于 2018-12-06T11:54:06.243 回答
0

特别感谢这里或多或少相同问题的答案。

对我来说,我需要的只是setlocale(LC_ALL, "en_US.UTF-8");

然后,我什至可以使用原始wchar_t字符。

于 2020-09-14T06:44:13.563 回答
0

在 Linux 上,Unicode 字符(UTF-16 / UTF-32)可以转换为 UTF-8 并打印到 std::cout。我使用了这些功能

于 2021-05-23T09:34:46.613 回答