JavaScript
Convert ArrayBufferView into UTF-8 String

by Benny Neugebauer

const text = 'Hello';
const buffer = new ArrayBuffer(text.length);
let view = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
  view[i] = text.charCodeAt(i);
}
const unicodeString = new TextDecoder("utf-8").decode(view); // "Hello"

const text = 'Hello';
const buffer = new ArrayBuffer(text.length);
let view = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
  view[i] = text.charCodeAt(i);
}
const unicodeString = String.fromCharCode.apply(null, view); // "Hello"

MS Edge 17 doesn't support TextDecoder, so it's recommended to use String.fromCharCode.apply in that case. :)

Benny Neugebauer

Sign in to comment!