Last active
June 7, 2016 15:08
-
-
Save nytr0gen/f604b6336a6bb219dfcf55e4dbaeaa4e to your computer and use it in GitHub Desktop.
Encode non-printable characters as hex
This file contains hidden or 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
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