Friday, May 9, 2008

Convert an integer to an ascii character in Javascript #2

In my previous post titled Convert an integer to an ascii character in Javascript, I said, I could not find any built-in methods in Javascript to convert a char into an integer and vice versa, well, it turns out that I have not looked hard enough. There are two methods on (one static and one instance as expected) String class that would do this conversion and these method are available since Javascript 1.2. I am sorry about the overlook. Here are sample functions to illustrate how these methods can be used in your code.

// Converts an integer (unicode value) to a char
function itoa(i)
   return String.fromCharCode(i);

// Converts a char into to an integer (unicode value)
function atoi(a)
   return a.charCodeAt();




Sakthipriyan, said...

Hi Sonny,

Thanks a lot. Your ideas helped me much....

fschwiet said...


Anonymous said...

gracias por tu aportacion.
thanks for your code