将UTF-8 BOM添加到字符串/ Blob [英] Adding UTF-8 BOM to string/Blob

查看:837
本文介绍了将UTF-8 BOM添加到字符串/ Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在客户端为生成的文本数据添加UTF-8字节顺序标记。我该怎么办?

I need to add a UTF-8 byte-order-mark to generated text data on client side. How do I do that?

使用新Blob(['\ xEF \ xBB \ xBF'+内容])收益率'我的数据',当然。

也没有'\ uBBEF \ x22BF'工作('\ x22'==''''成为下一个角色 content )。

Neither did '\uBBEF\x22BF' work (with '\x22' == '"' being the next character in content).

是否可以将JavaScript中的UTF-8 BOM添加到生成的文本中?

Is it possible to prepend the UTF-8 BOM in JavaScript to a generated text?

是的,我确实需要UTF-8 BOM。

推荐答案

\ ufeff 添加到字符串中。参见 http://msdn.microsoft.com/en-us/library/ie/2yfce773(v = vs.94).aspx

Prepend \ufeff to the string. See http://msdn.microsoft.com/en-us/library/ie/2yfce773(v=vs.94).aspx

请参阅 @ jeff-fischer @casey 有关UTF-8和UTF-16的详细信息和BOM。实际上,上述工作是字符串 \ ufeff 始终用于表示BOM,无论使用的是UTF-8还是UTF-16。

See discussion between @jeff-fischer and @casey for details on UTF-8 and UTF-16 and the BOM. What actually makes the above work is that the string \ufeff is always used to represent the BOM, regardless of UTF-8 or UTF-16 being used.

参见 Unicode标准5.0,第2章详细说明。该页面的引用

See p.36 in The Unicode Standard 5.0, Chapter 2 for a detailed explanation. A quote from that page


表2-4中UTF-8的endian订单条目标记为N / A,因为
UTF-8代码单元的大小为8位,较大代码单元的通常机器问题
endian顺序不适用。字节的序列化订单
不得偏离UTF-8
编码表单定义的顺序。
UTF-8既不要求也不建议使用BOM,但在使用BOM的其他编码表格或BOM为$ b的UTF-8数据为
的情况下可能会遇到$ b用作UTF-8签名。

The endian order entry for UTF-8 in Table 2-4 is marked N/A because UTF-8 code units are 8 bits in size, and the usual machine issues of endian order for larger code units do not apply. The serialized order of the bytes must not depart from the order defined by the UTF- 8 encoding form. Use of a BOM is neither required nor recommended for UTF-8, but may be encountered in contexts where UTF-8 data is converted from other encoding forms that use a BOM or where the BOM is used as a UTF-8 signature.

这篇关于将UTF-8 BOM添加到字符串/ Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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