Skip to content

Instantly share code, notes, and snippets.

@skratchdot
Created March 3, 2016 04:43
Show Gist options
  • Save skratchdot/e095036fad80597f1c1a to your computer and use it in GitHub Desktop.
Save skratchdot/e095036fad80597f1c1a to your computer and use it in GitHub Desktop.

Revisions

  1. skratchdot created this gist Mar 3, 2016.
    4 changes: 4 additions & 0 deletions arrayBufferToString.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    // source: http://stackoverflow.com/a/11058858
    function ab2str(buf) {
    return String.fromCharCode.apply(null, new Uint16Array(buf));
    }
    9 changes: 9 additions & 0 deletions stringToArrayBuffer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // source: http://stackoverflow.com/a/11058858
    function str2ab(str) {
    var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
    }
    return buf;
    }