Skip to content

Instantly share code, notes, and snippets.

@donpark
Created July 13, 2013 16:00
Show Gist options
  • Save donpark/5991178 to your computer and use it in GitHub Desktop.
Save donpark/5991178 to your computer and use it in GitHub Desktop.
Convert JavaScript string to UTF-8 bytes in modern browsers using the File API
// Convert JavaScript string to UTF-8 bytes in modern browsers using the File API
// callback function will be called with UTF8 bytes in Uint8Array
//
// jsPerf test reveals this method is slower than looping over each character.
// http://jsperf.com/convert-javascript-string-to-utf-8-bytes-using-file-api
function toUTF8(string, cb) {
var reader = new FileReader();
reader.onload = function(evt) {
cb(new Uint8Array(reader.result));
};
reader.readAsArrayBuffer(new Blob([string]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment