1.10 Converting Between Unicode Values and String Characters
NN 2, IE 3
You want to obtain the
Unicode code number for an alphanumeric character or vice versa.
To obtain the Unicode value of a character of a string, use the
charCodeAt( ) method of the string value. A single
parameter is an integer pointing to the zero-based position of the
character within the string:
var code = myString.charCodeAt(3);
If the string consists of only one character, use the
0 argument to get the code for that one character:
var oneChar = myString.substring(12, 13);
var code = oneChar.charCodeAt(0);
The returned value is an integer.
To convert an Unicode code number to a character, use the
fromCharCode( ) method of the static
var char = String.fromCharCode(66);
Unlike most string methods, this one must be invoked only from the
String object and not from a string value.
ASCII values and Unicode values are the same for the basic Latin
alphanumeric (low-ASCII) values. But even though Unicode encompasses
characters from many written languages around the world, do not
expect to see characters from other writing systems displayed in
alert boxes, text boxes, or rendered pages simply because you know
the Unicode values for those characters; the browser and operating
system must be equipped for the language encompassed by the
characters. If the character sets are not available, the characters
generated by such codes will be question marks or other symbols. A
typical North American computer won't know how to
produce a Chinese character on the screen unless the target writing
system and font sets are installed for the OS and browser.
1.10.4 See Also
Recipe 1.2 for other ways to extract single-character substrings.