Skip to content

Instantly share code, notes, and snippets.

@nytr0gen
Last active June 7, 2016 15:08
Show Gist options
  • Save nytr0gen/f604b6336a6bb219dfcf55e4dbaeaa4e to your computer and use it in GitHub Desktop.
Save nytr0gen/f604b6336a6bb219dfcf55e4dbaeaa4e to your computer and use it in GitHub Desktop.
Encode non-printable characters as hex
var encodeNP = function(s){
var hex, c;
var result = '';
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
if (c >= 32 && c <= 126) {
result += String.fromCharCode(c);
} else {
hex = c.toString(16);
result += '\\x' + ('000'+hex).slice(-4);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment