我搜索了很多,但找不到任何东西:
unsigned int unicodeChar = 0x5e9;
unsigned int utf8Char;
uni2utf8(unicodeChar, utf8Char);
assert(utf8Char == 0xd7a9);
是否有实现类似于 uni2utf8 的库(最好是 boost)?
最佳答案
Unicode 转换是 C++11 的一部分:
#include <codecvt>
#include <locale>
#include <string>
#include <cassert>
int main() {
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
std::string utf8 = convert.to_bytes(0x5e9);
assert(utf8.length() == 2);
assert(utf8[0] == '\xD7');
assert(utf8[1] == '\xA9');
}
关于c++ - C++ 中的 Unicode 到 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11603400/