字符的 UTF-8 转换
UTF-8 conversion for characters
我目前有一个std::string
,它包含这个
"xa9 2006 FooWorld"
基本上它包含符号 © .此字符串将传递给采用 UTF-8 的外部 API 的方法。如何使此字符串 UTF-8 兼容?任何建议。我在这里读到我可以使用std::wstring_convert
但我不确定如何在我的情况下应用它。任何建议将不胜感激。
这很简单:使用 UTF-8 字符串文字:
u8"u00A9 2006 FooWorld"
这将导致const char[]
是正确编码的 UTF-8 字符串。
在 C++11 及更高版本中,获取 UTF-8 编码字符串文本的最佳方法是使用u8
前缀:
std:string str = u8"u00A9 2006 FooWorld";
或:
std:string str = u8"© 2006 FooWorld";
但是,您也可以使用std::wstring_convert
,(特别是如果您的输入数据不是字符串文字):
#include <codecvt>
#include <locale>
#include <string>
std::wstring wstr = L"© 2006 FooWorld"; // or whatever...
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
std::string str = convert.to_bytes(wstr);
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 转换特殊字符(UTF-8)
- 如何使用 C++将 ISO-2022-KR 编码转换为 UTF-8 编码?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 在基于英语的系统上将 UTF-8 路径转换为宽字符会引发异常
- C++ 将 UTF-8 转换为字符串
- 从/到 UTF-8/UTF-16 的转换需要(例如:utf8 -> 代码点,然后代码点到 utf16)或(例如:utf8 -> utf16)?
- 无法将字符数组转换为包含 utf-8 字符的字符串
- 在 node.js 中将缓冲区从 C++ 转换为 UTF-8 字符串
- 从 ToUnicodeEx() 转换为 UTF-8
- 字符的 UTF-8 转换
- 如何将 PyObject 转换为 UTF-8 字符串
- 如何将代码点转换为 utf-8
- 将转义的 UTF-8 八位字节的字符数组转换为 C++ 的字符串
- "iconv"从 UTF-32LE 转换为 UTF-16LE
- 如何将 utf 字符转换为 windows-1252?
- 在 Linux 中将 UTF-32 宽字符转换为 UTF-16 宽字符以获取补充平面字符
- 将分数(1/8、3/8、5/8、7/8)转换为C 中的UTF-8
- 如何将UTF-8字符串转换为流的编码