Skip to content

Instantly share code, notes, and snippets.

@s1r-J
Last active August 16, 2021 12:42
Show Gist options
  • Save s1r-J/38ed191cc0ed12ea49e16fa9f9732836 to your computer and use it in GitHub Desktop.
Save s1r-J/38ed191cc0ed12ea49e16fa9f9732836 to your computer and use it in GitHub Desktop.
function arraybuffer2str(ab) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
function str2arraybuffer(str) {
let buf = new ArrayBuffer(str.length * 2);
let bufView = new Uint16Array(buf);
for (let i = 0; i < str.length; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment