节点缓冲区别名-二进制是latin1吗? [英] Node Buffer Alias - binary is latin1?

查看:105
本文介绍了节点缓冲区别名-二进制是latin1吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此页面


'binary'-'latin1'的别名。

'binary' - Alias for 'latin1'.

但是,二进制不是在latin1中可表示,因为缺少某些代码点。因此,像我这样的开发人员想要使用NodeJS缓冲区存储二进制数据(一种极为常见的用例),可能希望使用二进制作为编码。似乎没有任何文档可以正确解释如何处理二进制数据!我试图弄清这一点。

However, binary is not representable in latin1 as there are certain codepoints which are missing. So, a developer like me who wants to use NodeJS buffers for binary data (an extremely common use-case) would expect to use 'binary' as the encoding. There doesn't appear to be any documentation that properly explains how to handle binary data! I'm trying to make sense of this.

所以我的问题是:为什么选择latin1作为二进制的别名?

人们提到使用null作为编码将适用于二进制数据。因此有一个后续问题:为什么null和'binary'不会做同样的事情?

People have mentioned that using null as the encoding will work for binary data. So a followup question: Why doesn't null and 'binary' do the same thing?

推荐答案

Node文档对问题中所引用的二进制的定义上方的一行上的 latin1不是ISO 8859-1。它是:

The Node documentation's definition of 'latin1', on the line above the definition of 'binary' cited in the question, is not ISO 8859-1. It is:



  • 'latin1'-一种将Buffer编码为一字节编码字符串的方法b $ b(由IANA在RFC1345,第63页中定义,是Latin-1
    补充块和C0 / C1控制代码)。

RFC 1345中指定的 latin1字符集定义了所有256个代码点的映射。它没有ISO 8859-1映射中0x00-0x1f和0x7f-0x9f处的孔。

The 'latin1' charset specified in RFC 1345 defines mappings for all 256 codepoints. It does not have the holes that exist at 0x00-0x1f and 0x7f-0x9f in the ISO 8859-1 mapping.


为什么不

Why doesn't null and 'binary' do the same thing?

节点没有null编码。如果调用 Buffer.from('foo',null),则得到的结果与调用 Buffer.from('foo' )。也就是说,将应用默认编码。默认编码为 utf8,很明显,它会产生与二进制编码不同的结果。

Node does not have a null encoding. If you call Buffer.from('foo', null) then you get the same result as if you had called Buffer.from('foo'). That is, the default encoding is applied. The default encoding is 'utf8', and clearly that can produce results that differ from the 'binary' encoding.

这篇关于节点缓冲区别名-二进制是latin1吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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