英文:
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论