Last active
December 26, 2015 18:29
-
-
Save vukicevic/7194856 to your computer and use it in GitHub Desktop.
Convert UTF-8 string to and from integer byte-array.
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
function arrayToString(input) { | |
return decodeURIComponent( | |
input.map(function(v) { | |
return '%'+v.toString(16); | |
}) | |
.join('') | |
); | |
}; | |
function stringToArray(input) { | |
return encodeURIComponent( | |
input.split('') | |
.map(function(v) { | |
return (v.charCodeAt(0) < 128) ? '%'+v.charCodeAt(0).toString(16) : v; | |
}) | |
.join('') | |
).replace(/%25/g,'%') | |
.slice(1) | |
.split('%') | |
.map(function(v) { | |
return parseInt(v, 16); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment