为什么要使用Unicode Signature Byte-Order-Mark(BOM)? [英] Why would I use a Unicode Signature Byte-Order-Mark (BOM)?

查看:202
本文介绍了为什么要使用Unicode Signature Byte-Order-Mark(BOM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是否过时?它们似乎是最糟糕的想法 - 嵌入文件的内容,没有人可以看到,但影响文件的功能。我不明白为什么我想要一个。

解决方案

在某些情况下,这是必要的,是的,因为有两个UTF-16的小端和大端实现。



当读取一个未知的UTF-16文件时,你如何知道使用哪一个?
唯一的解决方案是在文件中放置一些容易识别的标记,这不能被误认为其他任何东西,不管使用的是哪一种。



这是BOM的作用。



你需要一个吗?只有当你是1)使用UTF编码,其中字节顺序是一个问题(对UTF-16而言非常重要,但UTF8总是看起来是一样的,无论内容如何),文件将被共享外部应用程序。



如果您自己的应用程序是唯一要读取和写入文件的应用程序,则可以省略BOM,并简单地决定一次你将要使用字符串但是如果另一个应用程序必须读取该文件,它将不会提前知道字节顺序,所以添加BOM可能是个好主意。


Are these obsolete? They seem like the worst idea ever -- embed something in the contents of your file that no one can see, but impacts the file's functionality. I don't understand why I would want one.

解决方案

They're necessary in some cases, yes, because there are both little-endian and big-endian implementations of UTF-16.

When reading an unknown UTF-16 file, how can you tell which of the two is used? The only solution is to place some kind of easily identifiable marker in the file, which can never be mistaken for anything else, regardless of the endian-ness used.

That's what the BOM does.

And do you need one? Only if you're 1) using an UTF encoding where endianness is an issue (It matters for UTF-16, but UTF8 always looks the same regardless of endianness), and the file is going to be shared with external applications.

If your own app is the only one that's going to read and write the file, you can omit the BOM, and simply decide once and for all which endianness you're going to use. But if another application has to read the file, it won't know the endianness in advance, so adding the BOM might be a good idea.

这篇关于为什么要使用Unicode Signature Byte-Order-Mark(BOM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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