Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ASCIIfy–Hexify: A bookmarklet to find a character’s ASCII and hexadecimal value
/* ASCIIfy–Hexify bookmarklet
* Authors: Lucas Larson, 2018
* Steve Kangas, 1998
* Description: This is the JavaScript behind a bookmarklet to obtain the ASCII₁₀ value and hexadecimal value of any single character.
* Installation: See bottom
* Use: Tap the bookmarklet, enter any single character – lowercase n, for example – and the bookmarklet returns n’s ASCII and hexadecimal values:
* The ASCII number for “n” is 110,
* and its hexadecimal value is 6E.
* Credit to For
* David Rubin Confidence
* Steve Kangas The “Character to ASCII” bookmarklet
* Travis Decker The term “hexify” instead of “hexafy”
* John Tantalo The bookmarklet-as-a-gist idea
* Chris Zarate Bookmarkleter (with IIFE-fier!)
* Joey Twiddle Making bookmarklet development easier
// Anonymize the function
!function() {
// “Kangas” ends in s but isn’t plural, we’ll add an apostrophe as well as s for the possessive
// Steve Kangas’s “p5VgU2z1” becomes “asciify”
asciify = prompt ("Enter a character:", "");
// avoid NaN output by executing only if asciify has a value
if (asciify) {
// forbid entries of more than one character
if (asciify.length > 1) {
alert ("Please enter only one character.");
} else {
// .toString(16) via Matt Dunlap
// .toUpperCase() via Fabio Ferrari
alert ("The ASCII number for “" + asciify + "” is " + asciify.charCodeAt(0) + ",\nand its hexadecimal value is " + asciify.charCodeAt(0).toString(16).toUpperCase() + ".");
// Installing bookmarklets is famously tricky (see
// ), but when you’re ready,
// the URI to enter into the bookmark’s address field is everything below after
// the two forward slashes and space
// javascript:!function(){asciify=prompt(%22Enter%20a%20character:%22,%22%22),asciify%26%26(asciify.length%3E1%3Falert(%22Please%20enter%20only%20one%20character.%22):alert(%22The%20ASCII%20number%20for%20%E2%80%9C%22+asciify+%22%E2%80%9D%20is%20%22+asciify.charCodeAt(0)+%22,\nand%20its%20hexadecimal%20value%20is%20%22+asciify.charCodeAt(0).toString(16).toUpperCase()+%22.%22))}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment