Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Created April 24, 2023 10:46
Show Gist options
  • Save alekstar79/6b16750a9ed835553edeb56b2a7393ed to your computer and use it in GitHub Desktop.
Save alekstar79/6b16750a9ed835553edeb56b2a7393ed to your computer and use it in GitHub Desktop.
Convert String to ArrayBuffer
export function str2ab(str)
{
let buf = new ArrayBuffer(str.length * 2) // 2 bytes for each char
let bufView = new Uint16Array(buf)
for (let i = 0, strLen = str.length; i < strLen; 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