faqts : Computers : Programming : Languages : JavaScript : Language Core : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

101 of 111 people (91%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I get the character from the (ascii/iso/uni) char code?

Mar 8th, 2000 08:53
Martin Honnen,


The following function generates the character from the char code for 
char codes till 255:

  function charFromCharCode (charCode) {
    return unescape('%' + charCode.toString(16));
  }
  // example copyright symbol
  alert(charFromCharCode (169))

This works with all versions of JavaScript.

You can also use 2-digit hexadecimal escpape sequences e.g.
  alert('\xA9')
and 3-digit octal escape e.g.
  alert('\251')

JavaScript 1.2 introduced 
  String.fromCharCode(characterCode)
to find the character from its ascii/iso/unicode, as in 
  alert(String.fromCharCode(169))
so if you script for version 4+ browsers you don't need to custom 
function given at the beginning.
You can even pass in a list of char codes e.g.
  alert(String.fromCharCode(75,105,98,111,108,111,103,121))

With JavaScript 1.3 you can also use unicode escape sequences of the 
form
  '\uDDDD'
where
  DDDD
is the 4 digit hexadecimal char code value e.g.
  alert('\u00A9')
will also show the copyright symbol.