另一个代码页检测问题
好吧,在你用长矛向我扑来并带我离开代码页的燃烧战场之前,请注意,我并不是试图自动检测文本的代码页。我知道那是不可能的。 但我不知道是否可以自动检测代码页问题。以下面的例子为例。我有一个较大的文本(2-3 页)加上一个“默认”代码页。我尝试使用默认代码页解码文本。如果出现乱码,我会尝试使用另一个代码页来解码文本。所以问题是:是否有可能以某种方式检测乱码字符?
感谢您提前提供的帮助。 此致, 丹尼尔
OK, before you jump at me with spears and take me away to the burning battlefield of code pages, please note that I am not trying to auto-detect the code page of a text. I know that's not possible.
But what I do not know to be possible is to automatically detect a code page problem. Take the following example. I have a largish text (2-3 pages) plus a "default" code page. I try to decode the text with the default code page. If I get gibberish I try to decode the text with another code page. So the question is: is it possible to somehow detect gibberish characters?
Thanks for your kind help in advance.
Best Regards,
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为唯一实用的方法是为每个代码页手动定义某种“掩码”;定义您认为对每个代码页有效的所有字符值的结构。
然后,您可以检查页面是否包含此掩码中未包含的任何字符值。
制作面具需要大量的手工工作。创建一个包含每个字符的页面,然后使用适当的代码页显示它,然后查看哪些未“很好”地呈现。这是每个代码页的一次性活动,因此也许值得付出努力。
当然,如果有一种方法可以解析代码页,您可以自动生成此掩码...嗯...稍后再说。
尝试这个代码片段。它针对每个已知代码页测试字符 32-255。
我在调试器中环顾四周,注意到“?”如果源字符未包含在代码页中,则用作后备字符。通过检查“?” (并确保它不是“?”开始),代码假设代码页无法处理它。
DBCS 代码页可能需要更多关注,我没有看过。但请尝试以此作为起点。
正如我之前所描述的,我将使用这样的代码来构建一个初始“掩码”,然后根据看起来不错和不合适的内容手动调整该掩码。
I reckon that the only practical way is to manually define some kind of 'mask' for each code page; a structure that defines all of the character values that you consider valid for each of your code pages.
Then, you could check if the page contained any character values that weren't contained in this mask.
Building the mask would involve a fair bit of manual effort. Create a page with every character, then display it using the appropriate code page and then look to see which aren't rendered 'nicely'. It's a one-off activity for each code page, so perhaps worth the effort.
Of course, if there was a way to parse a code page, you could generate this mask automatically... Hmm... Back in a bit.
Try this code fragment. It tests the characters 32-255 against each known code page.
I was looking around in the debugger and noticed that '?' is used as a fall-back character if the source character is not included in the code page. By checking for '?' (and ensuring that it wasn't '?' to start with), the code assumes that the code page couldn't handle it.
DBCS code pages may need a bit more attention, I've not looked. But try this as a starting point.
I'd use code like this to build an initial 'mask', as I described earlier, and then manually adjust that mask based on what looked good and what didn't.