Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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("�"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading