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

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)                        break;        }        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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS