如何转换为C 中的UTF8字符串
How convert to utf8 string in c++
我有一个字符串输出,不一定是有效的UTF8。我必须将其传递给仅接受有效UTF8字符串的方法。
因此,我需要将输出转换为最接近的有效UTF8字符串,以删除无效字节或零件。我该如何在C 中这样做?我想不使用第三方库。
您应该使用icu::UnicodeString
方法fromUTF8(const StringPiece &utf8)
或toUTF8String(StringClass &result).
如果您确定您的字符串是有效的UTF-8,只有几个损坏的字节,http://utfcpp.sourceforge.net/可以解决这个问题。从页面: #include "utf8.h"
void fix_utf8_string(std::string& str) {
std::string temp;
utf8::replace_invalid(str.begin(), str.end(), back_inserter(temp));
str = temp;
}
在处理Unicode数据时,您不使用第三方库的要求几乎是不可能的,但是UTF8-CPP库是仅标题的,这是您所能获得的。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 在 C++ 中将 utf8 wstring 转换为窗口中的字符串
- C++ 中的波斯语字符 utf8
- 如何替换/忽略 C stdio.h getline() 中的无效 Unicode/UTF8 字符
- Libharu 中的 UTF8:嵌入字体真的有必要吗?
- C :如何支持UTF8中的替代字符
- UTF 16 到 UTF8,使用 C++ 中的 utf8 库
- 如何转换为C 中的UTF8字符串
- 如何轻松检测字符串中的 utf8 编码
- 从本地<String>计算一行中的 UTF8 字符
- 编码.Utf8将逗号转换为c#中的一些特殊字符
- .NET系统::字符串到存储在char*中的UTF8字节
- 在 c++ Windows 中打开 utf8 编码的文件名