Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created August 21, 2015 10:55
Show Gist options
  • Select an option

  • Save JavaScript-Packer/2a4997ce64624106926b to your computer and use it in GitHub Desktop.

Select an option

Save JavaScript-Packer/2a4997ce64624106926b to your computer and use it in GitHub Desktop.
Unicode and hexdecimal encoding so that less characters used when character code range is less than 256
function unicodeEscape(d) {
for (var b = "", e = 0, a, c; !isNaN(a = d.charCodeAt(e++)); ) c = a.toString(16),
b += 256 > a ? "\\x" + (15 < a ? "" :"0") + c :"\\u" + ("0000" + c).slice(-4);
return b;
}
alert(unicodeEscape("test"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment