Skip to content

Instantly share code, notes, and snippets.

@vukicevic
Last active December 26, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vukicevic/7194856 to your computer and use it in GitHub Desktop.
Save vukicevic/7194856 to your computer and use it in GitHub Desktop.
Convert UTF-8 string to and from integer byte-array.
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