另一个代码页检测问题

发布于 2024-11-28 22:30:51 字数 214 浏览 0 评论 0原文

好吧,在你用长矛向我扑来并带我离开代码页的燃烧战场之前,请注意,我并不是试图自动检测文本的代码页。我知道那是不可能的。 但我不知道是否可以自动检测代码页问题。以下面的例子为例。我有一个较大的文本(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-12-05 22:30:51

我认为唯一实用的方法是为每个代码页手动定义某种“掩码”;定义您认为对每个代码页有效的所有字符值的结构。

然后,您可以检查页面是否包含此掩码中未包含的任何字符值。

制作面具需要大量的手工工作。创建一个包含每个字符的页面,然后使用适当的代码页显示它,然后查看哪些未“很好”地呈现。这是每个代码页的一次性活动,因此也许值得付出努力。

当然,如果有一种方法可以解析代码页,您可以自动生成此掩码...嗯...稍后再说。

尝试这个代码片段。它针对每个已知代码页测试字符 32-255。

        StringBuilder source = new StringBuilder();

        for (int ix = 0; ix < 224; ix++)
        {
            source.Append((char)(ix + 32));
        }

        EncodingInfo[] encs = Encoding.GetEncodings();

        foreach (var encInfo in encs)
        {
            System.Console.WriteLine(encInfo.DisplayName);
            Encoding enc = Encoding.GetEncoding(encInfo.CodePage);

            var result = enc.GetBytes(source.ToString().ToCharArray());

            for (int ix = 0; ix < 224; ix++)
            {
                if (result[ix] == 63 && source[ix] != 63)
                {
                    // Code page translated character to '?'
                    System.Console.Write("{0:d}", source[ix]);
                }
            }
            System.Console.WriteLine();
        }

我在调试器中环顾四周,注意到“?”如果源字符未包含在代码页中,则用作后备字符。通过检查“?” (并确保它不是“?”开始),代码假设代码页无法处理它。

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.

        StringBuilder source = new StringBuilder();

        for (int ix = 0; ix < 224; ix++)
        {
            source.Append((char)(ix + 32));
        }

        EncodingInfo[] encs = Encoding.GetEncodings();

        foreach (var encInfo in encs)
        {
            System.Console.WriteLine(encInfo.DisplayName);
            Encoding enc = Encoding.GetEncoding(encInfo.CodePage);

            var result = enc.GetBytes(source.ToString().ToCharArray());

            for (int ix = 0; ix < 224; ix++)
            {
                if (result[ix] == 63 && source[ix] != 63)
                {
                    // Code page translated character to '?'
                    System.Console.Write("{0:d}", source[ix]);
                }
            }
            System.Console.WriteLine();
        }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文