advertisement -- please support sponsors

converting between characters and ASCII values

How can I convert characters to ASCII values and vice versa?

There are no functions in JavaScript that make it easy to obtain ASCII values of characters and vice versa, but there are a couple of tricks that can be used to achieve the same effect.

In the World Wide Web, there are certain characters which cannot be legally used in URL strings because of their special meanings. To avoid errors associated with these special characters, URLs are usually subjected to a process known as URL encoding before they are submitted to servers. When a special character is URL encoded, it is replaced by a percent sign (%) followed by a two-digit hexadecimal representation of the character's ASCII value. (If the ASCII value is less than 16, the hexadecimal number is 0-padded.) Alpha-numeric characters do not usually need to be URL-encoded, but many punctuation symbols and escape characters do.

JavaScript provides two functions, escape and unescape, that can be used to manipulate URL-encoded strings. (Notice that I use the word function, not method, because these functions are not associated with any object and are part of the JavaScript language.) The escape function accepts a single string parameter and returns the equivalent URL-encoded string; unescape works the same way but in reverse.

Thus, if you want to determine the character represented by a certain ASCII value, simply pass a three-character string, consisting of the percent sign and the ASCII value in hexadecimal, to the unescape method.

Unfortunately, the reverse process, determining the ASCII value of a character, is not quite as easy. Since alpha-numeric characters do not need to be escaped, the escape method leaves them unmodified. Thus, the escape method cannot be used to determine the ASCII values of all characters.

If you don't care about efficiency, you can still use the unescape method to accomplish the same thing. Simply loop through all 256 ASCII values, unescaping them one-at-a-time, until you find an ASCII value that produces a matching character. The following code demonstrates one way to do this. It's inefficient, but it works:

function ascii_value (c)
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
	return i;
Just to prove that this really works, here is a form that uses the above function to determine the ASCII value of any character you type in:*


ASCII value
*If you are using Netscape 3, there may be a bug in your JavaScript interpreter that prevents this form from working correctly under certain conditions. More information on this bug is available here.

Charlton Rose
3 Jan. 1997