如何在c++中将unicode代码点转换为utf-8?

2023-11-25

我有一个由 unicode 代码点组成的数组

unsigned short array[3]={0x20ac,0x20ab,0x20ac};

我只是希望将其转换为 utf-8 以使用 C++ 逐字节写入文件。

例子: 0x20ac 应转换为 e2 82 ac。

或者有没有其他方法可以直接在文件中写入unicode字符。


最后!使用 C++11!

#include <string>
#include <locale>
#include <codecvt>
#include <cassert>

int main()
{
    std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
    std::string u8str = converter.to_bytes(0x20ac);
    assert(u8str == "\xe2\x82\xac");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在c++中将unicode代码点转换为utf-8? 的相关文章

随机推荐