首页 > 解决方案 > 如何在 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/ASCIIhttps://en.wikipedia.org/wiki/Windows-1252

test[0] 将等于十进制 116 (=0x74),但显然对于 é/0xE9,test[1] 不等于十进制值 233。

那么如何正确识别 é 呢?

标签: c++extended-ascii

解决方案


改变

if (test[i]<32)

if (test[i] >= 0 && test[i] < 32)

chars 通常是有符号类型,并且0xE9是八位整数中的负值。


推荐阅读