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?
>Solution :
You are decoding [200] which is an invalid UTF-8 sequence and results in �
const decoder = new TextDecoder()
const array = new Uint8Array([200])
console.log(decoder.decode(array)) // prints �
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
const encoder = new TextEncoder()
const array = new Uint8Array([])
console.log(encoder.encode("�"))