简体   繁体   中英

How to print unicode codepoint?

How can I print unicode codepoint as unicode character in C++ (gcc/clang) on Linux? Let say I have something like this:

typedef uint32_t codepoint;
codepoint cp = somefunction();

How can I print cp as single unicode character? I have en_US.UTF-8 locale.

I've searched SO, I've tried: wcout, wstring, wchar_t, setlocale, codecvt (no existent in 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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