如何测量非 ASCII 字符的正确大小
How to measure the correct size of non-ASCII characters?
在下面的程序中,我试图测量带有非ASCII字符的字符串的长度。
但是,我不确定为什么size()
在使用非 ASCII 字符时没有打印正确的长度。
#include <iostream>
#include <string>
int main()
{
std::string s1 = "Hello";
std::string s2 = "इंडिया"; // non-ASCII string
std::cout << "Size of " << s1 << " is " << s1.size() << std::endl;
std::cout << "Size of " << s2 << " is " << s2.size() << std::endl;
}
输出:
Size of Hello is 5
Size of इंडिया is 18
现场演示魔杖盒。
> std::string::size
返回以字节为单位的长度,而不是以字符数为单位。第二个字符串使用 UNICODE 编码,因此每个字符可能需要几个字节。请注意,这同样适用于std::wstring::size
,因为它将取决于编码(它返回宽字符的数量,而不是实际字符:如果使用 UTF-16,它将匹配但不一定用于其他编码,更多在本答案中)。
要测量实际长度(以符号数量为单位),您需要知道编码,以便正确分离(从而计算)字符。例如,此答案可能对 UTF-8 有所帮助(尽管使用的方法已在 C++17 中弃用)。
UTF-8 的另一个选项是计算第一个字节的数量(归功于另一个答案):
int utf8_length(const std::string& s) {
int len = 0;
for (auto c : s)
len += (c & 0xc0) != 0x80;
return len;
}
我使用了 std::wstring_convert 类并获得了正确的字符串长度。
#include <string>
#include <iostream>
#include <codecvt>
int main()
{
std::string s1 = "Hello";
std::string s2 = "इंडिया"; // non-ASCII string
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cn;
auto sz = cn.from_bytes(s2).size();
std::cout << "Size of " << s2 << " is " << sz << std::endl;
}
现场演示魔杖盒。
此处的重要性参考链接,了解有关std::wstring_convert
的更多信息
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- RE2 不匹配非 ASCII 字符
- 打印从 0 到 255 的所有 ASCII 字符
- C++ 电话号码字母拨号程序 - 语法错别字?还是非 ASCII 字符?
- 无符号字符打印其 ASCII 值
- 如何从 QString 中包含的十六进制值中获取 ASCII 字符?
- 在结构数据类型中更改每个字符的 ASCII 值
- Cin 和 getline 无法正确保存非 ascii 字符
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 非ASCII字符的词典排序
- 将char类型指定给int以打印相应的ASCII字符
- 将非 ASCII 字符转换为英语对应字符C++
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- C++ UTF-8 瑞典语字符读取为 ASCII
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 如何遍历所有 ASCII 字符?
- 读取字符 ASCII 值并将其记录到矢量
- 如何在C++中读取带有ifstream的替换字符?(ASCII 中的 SUB)