Skip to content

Instantly share code, notes, and snippets.

@socheatsok78
Created April 5, 2024 06:09
Show Gist options
  • Select an option

  • Save socheatsok78/6f7c02d11eba18a5c2ec7c4f7582ca4d to your computer and use it in GitHub Desktop.

Select an option

Save socheatsok78/6f7c02d11eba18a5c2ec7c4f7582ca4d to your computer and use it in GitHub Desktop.

Revisions

  1. socheatsok78 created this gist Apr 5, 2024.
    7 changes: 7 additions & 0 deletions abtostr.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    /*
    Convert an ArrayBuffer into a string
    from https://developer.chrome.com/blog/how-to-convert-arraybuffer-to-and-from-string/
    */
    function ab2str(buf) {
    return String.fromCharCode.apply(null, new Uint8Array(buf));
    }
    12 changes: 12 additions & 0 deletions strtoab.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /*
    Convert a string into an ArrayBuffer
    from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
    */
    function str2ab(str) {
    const buf = new ArrayBuffer(str.length);
    const bufView = new Uint8Array(buf);
    for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
    }
    return buf;
    }