如何在C ++中将unicode代码点转换为utf-8? [英] How to convert unicode code points to utf-8 in c++?
本文介绍了如何在C ++中将unicode代码点转换为utf-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由unicode代码点组成的数组
I have an array consisting of unicode code points
unsigned short array[3]={0x20ac,0x20ab,0x20ac};
我只希望将其转换为utf-8以使用C ++逐字节写入文件.
I just want this to be converted as utf-8 to write into file byte by byte using C++.
示例: 0x20ac应转换为e2 82 ac.
Example: 0x20ac should be converted to e2 82 ac.
或者还有其他方法可以直接在文件中写入unicode字符.
or is there any other method that can directly write unicode characters in file.
推荐答案
最后!使用C ++ 11!
Finally! With 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");
}
这篇关于如何在C ++中将unicode代码点转换为utf-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文