C#:循环编码 [英] C#: Cycle through encodings

查看:130
本文介绍了C#:循环编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取各种格式和语言的文件,我目前正在使用一个小编码库来尝试检测正确的编码( http://www.codeproject.com/KB/recipes/DetectEncoding.aspx )。



很漂亮好,但它偶尔会错过。 (多语言文件)



我的大多数潜在用户对编码的理解很少(我最希望的是它与字符有关),非常不可能能够在列表中选择正确的编码,所以我想让他们循环通过不同的编码,直到找到正确的编码,只需点击一个按钮。



显示问题?点击这里尝试不同的编码!



这是什么最好的方式来实现这样的东西?






编辑:看起来我没有表达得很清楚。通过循环通过编码,我不是指如何循环编码?



我的意思是如何让用户在不重新加载文件的情况下尝试不同的编码?



这个想法更像这样:假设文件加载了错误的编码。显示一些奇怪的字符。用户将点击按钮下一个编码或上一个编码,并且该字符串将被转换为不同的编码。用户只需要持续点击,直到找到正确的编码。 (无论编码看起来好的用户会做得很好)。只要用户可以点击下一步,他就有合理的机会解决他的问题。



我发现到目前为止,涉及到使用当前编码,然后将字节转换为下一个编码,将这些字节转换为字符,然后将字符转换为字符串...可以,但我想知道是否有一个更简单的方法来做到这一点。



例如,如果有一个方法读取一个字符串,并使用不同的编码,如render(string,encoding)。 >




非常感谢您的回答!

解决方案

以字节读取文件,然后使用Encoding.GetString方法。

  byte [] data = System。 IO.File.ReadAllBytes(path); 

Console.WriteLine(Encoding.UTF8.GetString(data));
Console.WriteLine(Encoding.UTF7.GetString(data));
Console.WriteLine(Encoding.ASCII.GetString(data));

因此,您只能加载文件一次。您可以使用基于文件的原始字节的每个编码。用户可以选择正确的一个,你可以使用Encoding.GetEncoding(...)。GetString(data)的结果进一步处理。


I am reading files in various formats and languages and I am currently using a small encoding library to take attempt to detect the proper encoding (http://www.codeproject.com/KB/recipes/DetectEncoding.aspx).

It's pretty good, but it still misses occasionally. (Multilingual files)

Most of my potential users have very little understanding of encoding (the best I can hope for is "it has something to do with characters") and are very unlikely to be able to choose the right encoding in a list, so I would like to let them cycle through different encodings until the right one is found just by clicking on a button.

Display problems? Click here to try a different encoding! (Well that's the concept anyway)

What would be the best way to implement something like that?


Edit: Looks like I didn't express myself clearly enough. By "cycling through the encoding", I don't mean "how to loop through encodings?"

What I meant was "how to let the user try different encodings in sequence without reloading the file?"

The idea is more like this: Let's say the file is loaded with the wrong encoding. Some strange characters are displayed. The user would click a button "Next encoding" or "previous encoding", and the string would be converted in a different encoding. The user just need to keep clicking until the right encoding is found. (whatever encoding looks good for the user will do fine). As long as the user can click "next", he has a reasonable chance of solving his problem.

What I have found so far involves converting the string to bytes using the current encoding, then converting the bytes to the next encoding, converting those bytes into chars, then converting the char into a string... Doable, but I wonder if there isn't an easier way to do that.

For instance, if there was a method that would read a string and return it using a different encoding, something like "render(string, encoding)".


Thanks a lot for the answers!

解决方案

Read the file as bytes and use then the Encoding.GetString Method.

        byte[] data = System.IO.File.ReadAllBytes(path);

        Console.WriteLine(Encoding.UTF8.GetString(data));
        Console.WriteLine(Encoding.UTF7.GetString(data));
        Console.WriteLine(Encoding.ASCII.GetString(data));

So you have to load the file only one time. You can use every encoding based on the original bytes of the file. The user can select the correct one und you can use the result of Encoding.GetEncoding(...).GetString(data) for further processing.

这篇关于C#:循环编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆