查找任何文件编码的有效方法
是的,这是一个最常见的问题,这件事对我来说很模糊,因为我对此了解不多。
但我想要一种非常精确的方法来查找文件编码。 与 Notepad++ 一样精确。
Yes is a most frequent question, and this matter is vague for me and since I don't know much about it.
But i would like a very precise way to find a files Encoding.
So precise as Notepad++ is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
StreamReader.CurrentEncoding 属性很少为我返回正确的文本文件编码。通过分析文件的字节顺序标记 (BOM),我在确定文件的字节顺序方面取得了更大的成功。如果文件没有 BOM,则无法确定文件的编码。
*已更新 2020 年 4 月 8 日,包括 UTF-32LE 检测并返回 UTF-32BE 的正确编码
The
StreamReader.CurrentEncoding
property rarely returns the correct text file encoding for me. I've had greater success determining a file's endianness, by analyzing its byte order mark (BOM). If the file does not have a BOM, this cannot determine the file's encoding.*UPDATED 4/08/2020 to include UTF-32LE detection and return correct encoding for UTF-32BE
使用
StreamReader
类,以下代码对我来说效果很好:技巧是使用
Peek
调用,否则,.NET 不会执行任何操作(并且它还没有执行任何操作)阅读序言、BOM)。当然,如果您在检查编码之前使用任何其他ReadXXX
调用,它也可以工作。如果文件没有 BOM,则将使用
defaultEncodingIfNoBom
编码。还有一个没有此参数的 StreamReader 构造函数重载(在这种情况下,在任何读取之前编码将默认设置为 UTF8),但我建议在您的上下文中定义您认为默认的编码。我已经使用带有 UTF8、UTF16/Unicode(LE 和 BE)和 UTF32(LE 和 BE)BOM 的文件成功测试了这一点。它不适用于 UTF7。
The following code works fine for me, using the
StreamReader
class:The trick is to use the
Peek
call, otherwise, .NET has not done anything (and it hasn't read the preamble, the BOM). Of course, if you use any otherReadXXX
call before checking the encoding, it works too.If the file has no BOM, then the
defaultEncodingIfNoBom
encoding will be used. There is also aStreamReader
constructor overload without this argument (in this case, the encoding will by default be set to UTF8 before any read), but I recommend to define what you consider the default encoding in your context.I have tested this successfully with files with BOM for UTF8, UTF16/Unicode (LE & BE) and UTF32 (LE & BE). It does not work for UTF7.
提供 @CodesInChaos 提议的步骤的实现细节:
1) 检查是否有字节顺序标记
2) 检查文件是否有效 UTF8
3) 使用本地“ANSI”代码页(Microsoft 定义的 ANSI)
步骤 2 有效因为除了 UTF8 之外,代码页中的大多数非 ASCII 序列都不是有效的 UTF8。 https://stackoverflow.com/a/4522251/867248 更详细地解释了该策略。
Providing the implementation details for the steps proposed by @CodesInChaos:
1) Check if there is a Byte Order Mark
2) Check if the file is valid UTF8
3) Use the local "ANSI" codepage (ANSI as Microsoft defines it)
Step 2 works because most non ASCII sequences in codepages other that UTF8 are not valid UTF8. https://stackoverflow.com/a/4522251/867248 explains the tactic in more details.
检查一下。
UDE
这是 Mozilla 通用字符集检测器的端口,您可以像这样使用它......
Check this.
UDE
This is a port of Mozilla Universal Charset Detector and you can use it like this...
我会尝试以下步骤:
1)检查是否有字节顺序标记
2)检查文件是否有效 UTF8
3)使用本地“ANSI”代码页(Microsoft 定义的 ANSI)
步骤 2 有效,因为大多数非 ASCII UTF8 以外的代码页中的序列不是有效的 UTF8。
I'd try the following steps:
1) Check if there is a Byte Order Mark
2) Check if the file is valid UTF8
3) Use the local "ANSI" codepage (ANSI as Microsoft defines it)
Step 2 works because most non ASCII sequences in codepages other that UTF8 are not valid UTF8.
.NET 不是很有帮助,但您可以尝试以下算法:
这是调用:
这是代码:
.NET is not very helpful, but you can try the following algorithm:
Here is the call:
Here is the code:
@nonoandy 提出的解决方案非常有趣,我已经成功地测试了它并且似乎工作得很好。
所需的 nuget 包是
Microsoft.ProgramSynthesis.Detection
(目前版本 8.17.0)我建议使用
EncodingTypeUtils.GetDotNetName
而不是使用开关来获取 <代码>编码实例:The solution proposed by @nonoandy is really interesting, I have succesfully tested it and seems to be working perfectly.
The nuget package needed is
Microsoft.ProgramSynthesis.Detection
(version 8.17.0 at the moment)I suggest to use the
EncodingTypeUtils.GetDotNetName
instead of using a switch for getting theEncoding
instance:在此处查找 c#
https ://msdn.microsoft.com/en-us/library/system.io.streamreader.currentencoding%28v=vs.110%29.aspx
Look here for c#
https://msdn.microsoft.com/en-us/library/system.io.streamreader.currentencoding%28v=vs.110%29.aspx
以下代码是我的Powershell代码,用于确定某些cpp或h或ml文件是否使用ISO-8859-1(Latin-1)或不带BOM的UTF-8编码,如果两者都不是,则假设它是GB18030。我是一名在法国工作的中国人,MSVC 在法国计算机上保存为 Latin-1,在中国计算机上保存为 GB,因此这有助于我在系统和同事之间进行源文件交换时避免编码问题。
方法很简单,如果所有字符都在x00-x7E之间,ASCII、UTF-8和Latin-1都是一样的,但是如果我用UTF-8读取非ASCII文件,我们会发现特殊字符�出现,所以尝试用 Latin-1 来阅读。在Latin-1中,\x7F和\xAF之间是空的,而GB在x00-xFF之间使用完整的,所以如果我在两者之间有任何一个,它不是Latin-1
代码是用PowerShell编写的,但使用.net,所以很容易被翻译成 C# 或 F#
The following codes are my Powershell codes to determinate if some cpp or h or ml files are encodeding with ISO-8859-1(Latin-1) or UTF-8 without BOM, if neither then suppose it to be GB18030. I am a Chinese working in France and MSVC saves as Latin-1 on french computer and saves as GB on Chinese computer so this helps me avoid encoding problem when do source file exchanges between my system and my colleagues.
The way is simple, if all characters are between x00-x7E, ASCII, UTF-8 and Latin-1 are all the same, but if I read a non ASCII file by UTF-8, we will find the special character � show up, so try to read with Latin-1. In Latin-1, between \x7F and \xAF is empty, while GB uses full between x00-xFF so if I got any between the two, it's not Latin-1
The code is written in PowerShell, but uses .net so it's easy to be translated into C# or F#
这似乎运作良好。
首先创建一个辅助方法:
然后创建代码来测试源代码。在这种情况下,我有一个字节数组,我需要获取以下内容的编码:
This seems to work well.
First create a helper method:
Then create code to test the source. In this case, I've got a byte array I need to get the encoding of:
我尝试了几种不同的方法来检测编码并遇到大多数问题。
我利用 Microsoft Nuget 包制作了以下内容,到目前为止它似乎对我有用,但需要进行更多测试。
我的大部分测试都是针对 UTF8、带有 BOM 的 UTF8 和 ANSI。
I have tried a few different ways to detect encoding and hit issues with most of them.
I made the following leveraging a Microsoft Nuget Package and it seems to work for me so far but needs tested a lot more.
Most of my testing has been on UTF8, UTF8 with BOM and ANSI.
可能有用
It may be useful