UTF-8和UTF-8与BOM有什么不同?哪个更好?
当前回答
Unicode字节顺序标记(BOM)常见问题解答提供了一个简明的答案:
Q: How I should deal with BOMs? A: Here are some guidelines to follow: A particular protocol (e.g. Microsoft conventions for .txt files) may require use of the BOM on certain Unicode data streams, such as files. When you need to conform to such a protocol, use a BOM. Some protocols allow optional BOMs in the case of untagged text. In those cases, Where a text data stream is known to be plain text, but of unknown encoding, BOM can be used as a signature. If there is no BOM, the encoding could be anything. Where a text data stream is known to be plain Unicode text (but not which endian), then BOM can be used as a signature. If there is no BOM, the text should be interpreted as big-endian. Some byte oriented protocols expect ASCII characters at the beginning of a file. If UTF-8 is used with these protocols, use of the BOM as encoding form signature should be avoided. Where the precise type of the data stream is known (e.g. Unicode big-endian or Unicode little-endian), the BOM should not be used. In particular, whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used.
其他回答
这个问题已经有了无数个答案,其中许多答案都很好,但我想尝试并澄清何时应该使用BOM,何时不应该使用BOM。
如前所述,任何使用UTF BOM(字节顺序标记)来确定字符串是否为UTF-8的方法都是有根据的猜测。如果有适当的元数据可用(如charset="utf-8"),那么您已经知道应该使用什么,但除此之外,您还需要进行测试并做出一些假设。这涉及到检查字符串来自的文件是否以十六进制字节码EF BB BF开头。
If a byte code corresponding to the UTF-8 BOM is found, the probability is high enough to assume it's UTF-8 and you can go from there. When forced to make this guess, however, additional error checking while reading would still be a good idea in case something comes up garbled. You should only assume a BOM is not UTF-8 (i.e. latin-1 or ANSI) if the input definitely shouldn't be UTF-8 based on its source. If there is no BOM, however, you can simply determine whether it's supposed to be UTF-8 by validating against the encoding.
为什么不推荐使用BOM ?
不支持unicode或兼容性较差的软件可能会假定它是latin-1或ANSI,并且不会从字符串中剥离BOM,这显然会导致问题。 这并不是真正需要的(只要检查内容是否兼容,并且在找不到兼容编码时总是使用UTF-8作为备用)
什么时候应该使用BOM编码?
如果您无法以任何其他方式(通过字符集标记或文件系统元)记录元数据,并且像使用BOM一样使用程序,则应该使用BOM进行编码。在Windows上尤其如此,没有BOM的任何东西通常都被认为使用了遗留代码页。BOM告诉Office等程序,是的,这个文件中的文本是Unicode;这是使用的编码。
归根结底,我唯一真正有问题的文件是CSV。根据程序的不同,它必须或必须没有BOM。例如,如果你在Windows上使用Excel 2007+,如果你想要顺利地打开它,而不必求助于导入数据,它必须用BOM编码。
问:UTF-8和没有BOM的UTF-8有什么不同?哪个更好?
以下是一些摘自维基百科关于字节顺序标记(BOM)的文章,我相信这些文章为这个问题提供了一个可靠的答案。
关于BOM和UTF-8的含义:
Unicode标准允许使用UTF-8格式的BOM,但不要求 或推荐使用。字节顺序在UTF-8中没有意义,因此 在UTF-8中唯一使用的是在文本流开始时发出信号 以UTF-8编码。
不使用BOM的参数:
不使用BOM的主要动机是向后兼容性 使用不支持unicode的软件…另一个不这样做的原因 使用BOM是为了鼓励UTF-8作为“默认”编码。
使用BOM的参数:
The argument for using a BOM is that without it, heuristic analysis is required to determine what character encoding a file is using. Historically such analysis, to distinguish various 8-bit encodings, is complicated, error-prone, and sometimes slow. A number of libraries are available to ease the task, such as Mozilla Universal Charset Detector and International Components for Unicode. Programmers mistakenly assume that detection of UTF-8 is equally difficult (it is not because of the vast majority of byte sequences are invalid UTF-8, while the encodings these libraries are trying to distinguish allow all possible byte sequences). Therefore not all Unicode-aware programs perform such an analysis and instead rely on the BOM. In particular, Microsoft compilers and interpreters, and many pieces of software on Microsoft Windows such as Notepad will not correctly read UTF-8 text unless it has only ASCII characters or it starts with the BOM, and will add a BOM to the start when saving text as UTF-8. Google Docs will add a BOM when a Microsoft Word document is downloaded as a plain text file.
有或没有BOM,哪个更好:
IETF建议,如果一个协议(a)总是使用UTF-8, 或者(b)有其他方式表明使用的是什么编码, 那么它“应该禁止使用U+FEFF作为签名。”
我的结论是:
仅在与软件应用程序的兼容性是绝对必要的情况下使用BOM。
还要注意,虽然引用的维基百科文章指出,许多Microsoft应用程序依赖BOM来正确检测UTF-8,但并非所有Microsoft应用程序都是如此。例如,正如@barlop所指出的,当使用带有UTF-8†的Windows命令提示符时,此类类型和更多的命令不期望出现BOM。如果存在BOM,它可能会像其他应用程序一样出现问题。
†chcp命令通过代码页65001提供对UTF-8(没有BOM)的支持。
其他优秀的回答已经回答过了
UTF-8和BOM-ed的UTF-8之间没有官方的区别 一个BOM-ed的UTF-8字符串将以以下三个字节开始。Ef bb bf 如果存在这些字节,在从文件/流中提取字符串时必须忽略。
但是,作为附加信息,UTF-8的BOM可以很好地“嗅出”字符串是否以UTF-8编码……或者它可以是任何其他编码的合法字符串…
例如,数据[EF BB BF 41 42 43]可以是:
合法的ISO-8859-1字符串“ABC” 合法的UTF-8字符串“ABC”
因此,尽管通过查看第一个字节来识别文件内容的编码很酷,但您不应该依赖于此,如上面的示例所示
编码应该是已知的,而不是推测的。
引用于维基百科页面底部BOM: http://en.wikipedia.org/wiki/Byte-order_mark#cite_note-2
对于UTF-8,使用BOM既不要求也不推荐,但在从使用BOM的其他编码形式转换UTF-8数据或将BOM用作UTF-8签名的上下文中可能会遇到这种情况。
应该注意的是,对于某些文件,即使在Windows上也不能有BOM。例如SQL*plus或VBScript文件。如果这样的文件包含BOM,则在尝试执行它们时会出现错误。
推荐文章
- (grep)正则表达式匹配非ascii字符?
- 我如何确定文件编码在OS X?
- Java标识符中的“连接字符”是什么?
- 使用Javascript的atob解码base64不能正确解码utf-8字符串
- 为什么字符集名称不是常量?
- 编码字符串为UTF-8
- 有没有统一码符号来表示"搜索"
- 我如何在PHP中输出一个UTF-8 CSV, Excel将正确读取?
- Python __str__与__unicode__
- 如何在Python中将字符串转换为utf-8
- Unicode和UTF-8的区别是什么?
- 我真的需要将“&”编码为“&”吗?
- 用Python写入UTF-8文件
- c++中的_tmain()和main()有什么区别?
- HTML编码问题-显示“”字符而不是“ ”