Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created April 29, 2015 02:31
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 JavaScript-Packer/b82c839a56286de6d52c to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/b82c839a56286de6d52c to your computer and use it in GitHub Desktop.
Will give various representations of a single character
function characterWays(a) {
var decimal = a.charCodeAt(0);
var octal = decimal.toString(8);
var hex = decimal.toString(16);
var oct = "\\" + octal;
var hexValue = "0x" + hex;
var uniValue = "U+" + hex;
var escUni = "" + hex;
var escURL = "%" + hex;
var escHex = "\\x" + hex;
while (escUni.length < 4) escUni = "0" + escUni;
var escUniValue = "\\u" + escUni;
var htmlValue = "&#" + decimal + ";";
return htmlValue + " " + oct + " " + escURL + " " + escHex + " " + hexValue + " " + uniValue + " " + escUni + " " + escUniValue;
}
alert(characterWays("D"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment