UTF-8和UTF-8与BOM有什么不同?哪个更好?


当前回答

从http://en.wikipedia.org/wiki/Byte-order_mark:

字节顺序标记(BOM)是一个Unicode 符号的符号 文本文件的字节顺序 或流。其编码点为U+FEFF。 BOM使用是可选的,如果使用, 应该出现在文本的开头吗 流。除了它的特殊用途 字节顺序指示器,即BOM 字符也可以指示哪一个 几种Unicode表示 文本是用。

总是在文件中使用BOM将确保它总是在支持UTF-8和BOM的编辑器中正确打开。

我对缺少BOM的真正问题如下。假设我们有一个文件,它包含:

abc

如果没有BOM,在大多数编辑器中它会作为ANSI打开。所以这个文件的另一个用户打开它,并添加一些本机字符,例如:

abg-αβγ

哎呀……现在文件仍然在ANSI中,你猜怎么着,“αβγ”不占用6个字节,而是3个字节。这不是UTF-8,这会在开发链的后面引起其他问题。

其他回答

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.

UTF-8 BOM是文本流开头的字节序列(0xEF, 0xBB, 0xBF),它允许读者更可靠地猜测文件是否以UTF-8编码。

通常,BOM用于表示编码的字节顺序,但由于字节顺序与UTF-8无关,因此BOM是不必要的。

根据Unicode标准,不建议使用UTF-8文件的BOM:

2.6编码方案 ... 对于UTF-8,既不要求也不建议使用BOM,但在将UTF-8数据从使用BOM的其他编码形式转换或将BOM用作UTF-8签名的上下文中可能会遇到这种情况。有关更多信息,请参阅第16.8节特殊项中的“字节顺序标记”小节。

只有当文件实际包含一些非ascii字符时,UTF-8和BOM才有用。如果包含了它,而没有任何ASCII,那么它可能会破坏旧的应用程序,否则将文件解释为纯ASCII。当遇到非ASCII字符时,这些应用程序肯定会失败,因此在我看来,只有当文件可以并且不应该再被解释为纯ASCII时,才应该添加BOM。

我想说清楚的是,我宁愿没有BOM。如果一些旧的垃圾没有它就坏了,那么就添加它,替换遗留应用程序是不可行的。

不要制作UTF-8的BOM之外的任何东西。

如上所述,带有BOM的UTF-8可能会导致非BOM感知(或兼容)软件出现问题。我曾经用基于mozilla的KompoZer编辑UTF-8 + BOM编码的HTML文件,因为客户需要WYSIWYG程序。

保存时,布局总是会被破坏。我花了一些时间来解决这个问题。这些文件在Firefox中运行良好,但在Internet Explorer中显示了一个CSS怪癖,再次破坏了布局。在摆弄了几个小时链接的CSS文件后,我发现Internet Explorer不喜欢BOMfed HTML文件。我再也不会见你了。

还有,我刚在维基百科上找到了这个:

The shebang characters are represented by the same two bytes in extended ASCII encodings, including UTF-8, which is commonly used for scripts and other text files on current Unix-like systems. However, UTF-8 files may begin with the optional byte order mark (BOM); if the "exec" function specifically detects the bytes 0x23 0x21, then the presence of the BOM (0xEF 0xBB 0xBF) before the shebang will prevent the script interpreter from being executed. Some authorities recommend against using the byte order mark in POSIX (Unix-like) scripts,[15] for this reason and for wider interoperability and philosophical concerns

从http://en.wikipedia.org/wiki/Byte-order_mark:

字节顺序标记(BOM)是一个Unicode 符号的符号 文本文件的字节顺序 或流。其编码点为U+FEFF。 BOM使用是可选的,如果使用, 应该出现在文本的开头吗 流。除了它的特殊用途 字节顺序指示器,即BOM 字符也可以指示哪一个 几种Unicode表示 文本是用。

总是在文件中使用BOM将确保它总是在支持UTF-8和BOM的编辑器中正确打开。

我对缺少BOM的真正问题如下。假设我们有一个文件,它包含:

abc

如果没有BOM,在大多数编辑器中它会作为ANSI打开。所以这个文件的另一个用户打开它,并添加一些本机字符,例如:

abg-αβγ

哎呀……现在文件仍然在ANSI中,你猜怎么着,“αβγ”不占用6个字节,而是3个字节。这不是UTF-8,这会在开发链的后面引起其他问题。