convert-string
Version:
Convert to/from strings and array of bytes
29 lines (25 loc) • 746 B
JavaScript
!function(globals) {
var convertString = {
bytesToString: function(bytes) {
return bytes.map(function(x){ return String.fromCharCode(x) }).join('')
},
stringToBytes: function(str) {
return str.split('').map(function(x) { return x.charCodeAt(0) })
}
}
//http://hossa.in/2012/07/20/utf-8-in-javascript.html
convertString.UTF8 = {
bytesToString: function(bytes) {
return decodeURIComponent(escape(convertString.bytesToString(bytes)))
},
stringToBytes: function(str) {
return convertString.stringToBytes(unescape(encodeURIComponent(str)))
}
}
if (typeof module !== 'undefined' && module.exports) { //CommonJS
module.exports = convertString
} else {
globals.convertString = convertString
}
}(this);