Skip to content

Instantly share code, notes, and snippets.

@gabemeola
Created November 2, 2018 22:17
Show Gist options
  • Save gabemeola/c32b80d9302b30b577118c39f98e00b2 to your computer and use it in GitHub Desktop.
Save gabemeola/c32b80d9302b30b577118c39f98e00b2 to your computer and use it in GitHub Desktop.
/**
* Coverts a Javascript string to a
* unsigned 8 byte int ArrayBuffer
*
* @param {string} string - Binary String
* @returns {Uint8Array}
*/
export default function stringToArrayBuffer(string) {
const { length } = string;
const buffer = new Uint8Array(length);
for (let i = 0; i < length; i++) {
// Get char code
buffer[i] = string.charCodeAt(i);
}
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment