标准::字符串字符编码
std::string character encoding
std::string arrWords[10];
std::vector<std::string> hElemanlar;
......
this->hElemanlar.push_back(std::string(1, this->arrWords[sayKelime][j]).c_str());
......
我正在做的是:arrWord 的每个元素都是一个 std::string。我得到arrWord的第n个元素,然后将它们推入hElemanlar。
假设 arrWords[0] 是 "test",那么:
this->hElemanlar.push_back("t");
this->hElemanlar.push_back("e");
this->hElemanlar.push_back("s");
this->hElemanlar.push_back("t");
我的问题是,虽然我对arrWords没有编码问题,但一些utf-8字符在hElemanlar中没有打印或处理得很好。我该如何解决它?
如果您知道arrWords[i]
包含 UTF-8 编码文本,那么您可能需要将字符串拆分为完整的 Unicode 字符。
顺便说一句,而不是说:
this->hElemanlar.push_back(std::string(1, this->arrWords[sayKelime][j]).c_str());
(它构造一个临时的 std::string,获取它的 c 字符串表示,构造另一个临时字符串,并将其推送到向量上(,说:
this->hElemanlar.push_back(std::string(1, this->arrWords[sayKelime][j]))
无论如何。 这将需要变成这样的东西:
std::string str(1, this-arrWords[sayKelime][j])
if (static_cast<unsigned char>(str[0]) >= 0xC0)
{
for (const char c = this-arrWords[sayKelime][j+1];
static_cast<unsigned char>(c) >= 0x80;
j++)
{
str.push_back(c);
}
}
this->hElemenlar.push_back(str);
请注意,上面的循环是安全的,因为如果j
是字符串中最后一个字符的索引,[j+1]
将返回 nul-terminator(这将结束循环(。不过,您需要考虑递增 j 如何与其余代码交互。
然后,您需要考虑是希望hElemanlar
表示单个 Unicode 码位(这样做(,还是要包含一个字符 + 后面的所有组合字符? 在后一种情况下,您必须将上面的代码扩展到:
- 分析下一个代码点
- 确定它是否是组合字符
- 如果是这样,请推送字符串上的 UTF-8 序列。
- 重复(一个角色上可以有多个组合字符(。
相关文章:
- 是否有任何区域设置会影响宽字符编码?
- CP1251:从字符* 转换为 wchar_t* 时的编码失真
- 使用C++将越南语字符从ISO88591、UTF8、UTF16BE、UTF16LE和UTF16编码为十六进制,反之亦然
- 将二进制编码的无符号字符转换为整数
- 如何C++ WCOUT UTF-16 编码的字符数组?
- 字符编码和带有C++的文件名
- 如何在没有wchar_t的情况下在 c++ 中解码/编码 UTF-8 字符
- 如何定义一个从任何编码空间解析单个字符的精神 x3 解析器?
- C 14:UTF-8/UTF-16与本机字符编码之间的转换
- qt base64编码/decode添加逃脱字符
- 比较UTF8编码的字符
- 从键盘读取 UTF-8 编码的字符
- 如何获取 json 文件的字符编码类型
- 哪种方法更适合霍夫曼编码 我想读取字符及其频率
- Base64 在编码特殊字符时不起作用
- 编码一个字符
- C 和卷曲,发送电子邮件时编码奇怪的字符
- C 日食控制台编码纯文本作为其他字符
- 用于 pbm 文件的预编码字符和数字资源(便携式位图)
- 通过boost::asio传输多编码字符的最佳方式