How to convert unicode code points to utf-8 in c++?
c++, unicode, utf-8
Solution
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");
}
Problem
I have an array consisting of unicode code points ``` unsigned short array[3]={0x20ac,0x20ab,0x20ac}; ``` I just want this to be converted as utf-8 to write into file byte by byte using C++. Example: 0x20ac should be converted to e2 82 ac. or is there any other method that can directly write unicode characters in file.