解码和编码JS中的Typed Array会产生与原始值不同的结果。

huangapple go评论67阅读模式
英文:

Decoding and encoding Typed Array in JS giving different result than original value

问题

以下是翻译好的部分:

"Does anyone know why this happens"

"const encoder = new TextEncoder()
const decoder = new TextDecoder()
const array = new Uint8Array([200])
console.log(encoder.encode(decoder.decode(array))) // prints [239, 191, 189]"

"As far as I know, decoding and encoding anything should give me the exact same value. Should not it just print [200] back?"

英文:

Does anyone know why this happens

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    const encoder = new TextEncoder()
    const decoder = new TextDecoder()
    const array = new Uint8Array([200])
    console.log(encoder.encode(decoder.decode(array))) // prints [239, 191, 189]

<!-- end snippet -->

As far as I know, decoding and encoding anything should give me the exact same value. Should not it just print [200] back?

答案1

得分: 3

你正在解码 [200],这是一个无效的UTF-8序列,结果是

const decoder = new TextDecoder()
const array = new Uint8Array([200])
console.log(decoder.decode(array)) // 打印 �

由于200的二进制表示是11001000,解码器认为这是一个两字节的UTF-8字符:

在UTF-8编码中,以110开头的字节表示它是一个两字节编码的字符的第一个字节。

然后当你尝试编码 时,你会得到以下输出 [239, 191, 189] 或者用十六进制表示为 0xEF 0xBF 0xBD

const encoder = new TextEncoder()
const array = new Uint8Array([])
console.log(encoder.encode("�"))
英文:

You are decoding [200] which is an invalid UTF-8 sequence and results in

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const decoder = new TextDecoder()
const array = new Uint8Array([200])
console.log(decoder.decode(array)) // prints �

<!-- end snippet -->

Since the binary representation of 200 is 11001000, the decoder thinks this is a two-byte UTF-8 character:

> In UTF-8 encoding, a byte starting with 110 indicates that it's the first byte of a two-byte encoded character.

Then when you try to encode � you just get the following output [239, 191, 189] or 0xEF 0xBF 0xBD in hexadecimal

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const encoder = new TextEncoder()
const array = new Uint8Array([])
console.log(encoder.encode("�"))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 07:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定