Skip to content

Instantly share code, notes, and snippets.

@jarrodbell
Last active December 25, 2015 18:19
Show Gist options
  • Save jarrodbell/7019329 to your computer and use it in GitHub Desktop.
Save jarrodbell/7019329 to your computer and use it in GitHub Desktop.
Convert byte array (or string) to a readable "\xFF" formatted string for any bytes outside the printable ascii range (32-127 decimal)
function makeReadable(bytes) {
var readable = "", i;
for (i = 0; i < bytes.length; i++) {
var byteVal = bytes.charCodeAt(i);
if (byteVal < 32 || byteVal > 127) {
readable += "\\x" + ("0" + byteVal.toString(16).toUpperCase()).slice(-2);
} else {
readable += bytes[i];
}
}
return readable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment