简体   繁体   English

将字符串转换为缓冲区并返回 javascript 时出现无关字符

[英]Extraneous character while converting string to buffer and back in javascript

In node v16.14.2,在节点 v16.14.2 中,

> String.fromCharCode.apply(null, Buffer.from('°'))
'°'

This works fine though:不过,这很好用:

> Buffer.from('°').toString('utf-8')
'°'

I want to know why the first scenario adds an extra character in the output.我想知道为什么第一个场景在 output 中添加了一个额外的字符。

Browser example:浏览器示例:

 console.log(String.fromCharCode.apply(null, ethereumjs.Buffer.Buffer.from('°'))) /// '°' console.log(ethereumjs.Buffer.Buffer.from('°').toString('utf-8')) // '°' console.log(new Uint8Array(ethereumjs.Buffer.Buffer.from('°'))) // [ 194, 176 ] console.log(new Uint8Array(ethereumjs.Buffer.Buffer.from(String.fromCharCode(176)))) // [ 194, 176 ]
 <script src="https://cdn.jsdelivr.net/gh/ethereumjs/browser-builds/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>

EDIT : Another funny thing:编辑:另一个有趣的事情:

> new Uint8Array(Buffer.from('°'))
Uint8Array(2) [ 194, 176 ]
> new Uint8Array(Buffer.from(String.fromCharCode(176)))
Uint8Array(2) [ 194, 176 ]

The default string encoding of Buffer.from is UTF-8 which encodes characters in the Extended ASCII range using two bytes. Buffer.from 的默认字符串编码是Buffer.from ,它使用两个字节对扩展 ASCII 范围内的字符进行编码。 (It can't just use one byte because the highest order bit of that byte is used to signal multibyte encoding.) (它不能只使用一个字节,因为该字节的最高位用于表示多字节编码。)

UTF-8 two-byte encoding has the form: UTF-8 两字节编码形式为:

110xxxxx 10xxxxxx

which gives 11 bits to represent a character code.它给出了 11 位来表示一个字符代码。

The ASCII code of '°' is 176 '°'的 ASCII 码是 176

> '°'.charCodeAt(0)
176

which in binary is:二进制是:

> (176).toString(2)
'10110000'

So replacing the x with this binary value we have:所以用这个二进制值替换 x 我们有:

110xxxxx 10xxxxxx
110xxx10 10110000
      ^^   ^^^^^^

which means the UTF-8 two-byte encoding of '°' is这意味着'°'的 UTF-8 两字节编码是

11000010 10110000

or或者

194 176

as expected:正如预期的那样:

> [...Buffer.from('°')]
[ 194, 176 ]

> String.fromCharCode(194)
'Â'

> String.fromCharCode(176)
'°'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM