Created
July 13, 2013 16:00
-
-
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
This file contains 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
// 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