简体   繁体   English

如何打印unicode代码点?

[英]How to print unicode codepoint?

How can I print unicode codepoint as unicode character in C++ (gcc/clang) on Linux? 如何在Linux上的C ++(gcc / clang)中将unicode代码点打印为unicode字符? Let say I have something like this: 假设我有这样的事情:

typedef uint32_t codepoint;
codepoint cp = somefunction();

How can I print cp as single unicode character? 如何将cp打印为单个unicode字符? I have en_US.UTF-8 locale. 我有en_US.UTF-8语言环境。

I've searched SO, I've tried: wcout, wstring, wchar_t, setlocale, codecvt (no existent in gcc). 我搜索过,所以尝试了:wcout,wstring,wchar_t,setlocale,codecvt(gcc中不存在)。

std::wcout in GNU has a little quirk : while synchronized with C stdio, both C++ and CI/O subsystems need to be localized separately: GNU中的std::wcout 有点 std::wcout :与C stdio同步时,C ++和CI / O子系统都需要分别本地化:

so either unsync 所以要么不同步

#include <iostream>
#include <cstdint>
#include <locale>
int main()
{
    std::uint32_t n = 0x98A8;

    std::wcout.sync_with_stdio(false);
    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << wchar_t(n) << '\n';
}

http://coliru.stacked-crooked.com/a/13b718ae11fa539e http://coliru.stacked-crooked.com/a/13b718ae11fa539e

or localize both 或同时本地化

#include <iostream>
#include <cstdint>
#include <locale>
#include <clocale>
int main()
{
    std::uint32_t n = 0x98A8;

    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << wchar_t(n) << '\n';
}

http://coliru.stacked-crooked.com/a/80b7d4547e1184ad http://coliru.stacked-crooked.com/a/80b7d4547e1184ad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM