c++ - 如何在 C++ 中获取 Windows-1252 字符值?
问题描述
我有一个奇怪的输入文件,其中包含各种控制字符,例如空值。我想从这个 Windows-1252 编码的文本文件中删除所有控制字符,但如果你这样做:
std::string test="tést";
for (int i=0;i<test.length();i++)
{
if (test[i]<32) test[i]=32; // change all control characters into spaces
}
它也会将 é 更改为空格。
因此,如果您有这样的字符串,在 Windows-1252 中编码:
std::string test="tést";
十六进制值为:
t é s t
74 E9 73 74
请参阅https://en.wikipedia.org/wiki/ASCII和https://en.wikipedia.org/wiki/Windows-1252
test[0] 将等于十进制 116 (=0x74),但显然对于 é/0xE9,test[1] 不等于十进制值 233。
那么如何正确识别 é 呢?
解决方案
改变
if (test[i]<32)
到
if (test[i] >= 0 && test[i] < 32)
chars 通常是有符号类型,并且0xE9
是八位整数中的负值。
推荐阅读
- c# - 编译 C# WPF 项目时的 MessageBox
- javascript - 单选按钮选择具有相同名称的其他组按钮
- php - 使用 PHP 向 Android 应用推送通知
- java - 杰克逊禁用@JsonFormat注解
- uitabbar - 无法在 tvOS 13 上设置 UITabBarleadingAccessoryView
- vue.js - 如何组织嵌套的 axios 调用以使其更具可读性?
- python - 来自 scipy.interpolate 的 bisplrep 和 bisplev 的问题
- elasticsearch - 检查elasticsearch查询结果是否来自缓存?
- c# - Grapher COM 连接到 C#
- python-2.7 - Python:将文件转换为二进制文件的 tarfile 库