UTF-8和UTF-8与BOM有什么不同?哪个更好?
当前回答
当您希望显示以UTF-8编码的信息时,可能不会遇到问题。例如,将HTML文档声明为UTF-8,您将在浏览器中显示文档主体中包含的所有内容。
但在Windows或Linux上,当我们有文本、CSV和XML文件时,情况就不同了。
例如,Windows或Linux中的文本文件,这是最简单的事情之一,它(通常)不是UTF-8。
保存为XML并声明为UTF-8:
<?xml version="1.0" encoding="UTF-8"?>
即使声明为UTF-8,它也不能正确显示(不能读取)。
我有一串包含法语字母的数据,需要将其保存为XML以进行联合。无需从一开始就创建UTF-8文件(更改IDE中的选项和“创建新文件”)或在文件开头添加BOM
$file="\xEF\xBB\xBF".$string;
我无法将法语字母保存在XML文件中。
其他回答
如上所述,带有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
其他优秀的回答已经回答过了
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”
因此,尽管通过查看第一个字节来识别文件内容的编码很酷,但您不应该依赖于此,如上面的示例所示
编码应该是已知的,而不是推测的。
一个实际的区别是,如果你为Mac OS X编写一个shell脚本,并将其保存为普通的UTF-8,你将得到响应:
#!/bin/bash: No such file or directory
在shebang行指定您希望使用哪个shell的响应中:
#!/bin/bash
如果你保存为UTF-8,没有BOM(说在BBEdit),一切都会很好。
只有当文件实际包含一些非ascii字符时,UTF-8和BOM才有用。如果包含了它,而没有任何ASCII,那么它可能会破坏旧的应用程序,否则将文件解释为纯ASCII。当遇到非ASCII字符时,这些应用程序肯定会失败,因此在我看来,只有当文件可以并且不应该再被解释为纯ASCII时,才应该添加BOM。
我想说清楚的是,我宁愿没有BOM。如果一些旧的垃圾没有它就坏了,那么就添加它,替换遗留应用程序是不可行的。
不要制作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.
推荐文章
- (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编码问题-显示“”字符而不是“ ”