c++ - C++ 中的 Unicode 到 UTF-8

标签 c++ boost unicode utf-8

我搜索了很多,但找不到任何东西:

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/

相关文章:

c++ - boost binomial_coefficient 的日志版本?

c++ - 使用 CMake 和 Boost 单元测试进行编译

C++ boost函数问题

python - 为什么 Python 会自动将字符串中的十六进制编码为 UTF-8?

java - 如何在tomcat中使用UTF-8

c++ - 在 QT 中从一个 QToolButton 切换到另一个

c++ - 如何将ios设备联系人与qt app同步?

C++ OpenMP exit while loop inside a parallel for

html - Unicode 作为元素符号不缩进第二行

c++ - C++中的 “inline”关键字