- 
      
- 
        Save smhemel/f88e26ef6c31c0c76388f2c9ed487e89 to your computer and use it in GitHub Desktop. 
    Array Buffer -> String and String -> ArrayBuffer conversions in javascript
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // source: http://stackoverflow.com/a/11058858 | |
| function ab2str(buf) { | |
| return String.fromCharCode.apply(null, new Uint16Array(buf)); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment