Click to See Complete Forum and Search --> : Text to ASCII


S1L3NC3
10-05-2003, 02:26 PM
Is there a method in Javascript which you can use to convert a char to its ASCII value?

While were at it, how to convert an ASCII value back to its Text value?


Thanks

AdamGundry
10-05-2003, 02:36 PM
There's probably a better way to do this, but you can use charCodeAt(), like this: "A".charCodeAt() - returns 65. This also works for Unicode characters (values above 128).

You can use String.fromCharCode() to convert from integer value to text, like this: String.fromCharCode(65) - returns "A".

See the documentation on String: http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/string.html

Adam

Charles
10-05-2003, 03:27 PM
Originally posted by AdamGundry
This also works for Unicode characters (values above 128).Unicode is identical to ASCII from 0 to 127, then it skips to 226. The reason for that is that with utf-8 encoding the high order bit is a signal that the byte isn't a character but a description of how many bytes are to be taken as this one character. Windows-1252 uses these characters for other things.

S1L3NC3
10-05-2003, 04:27 PM
Thanks alot Adam, Im just goin to throw a val(x) in there instead of a char or number and hopefully itll work. Dont see why it wouldnt.

AdamGundry
10-06-2003, 10:46 AM
Thanks for the clarification, Charles - I am familiar with the distinction and should have explained better.

S1L3NC3, I don't know what you mean by using val(), as it is not a Javascript function AFAIK. Perhaps you are thinking of parseInt(), which converts a string representation of a number to an actual number? That would not be helpful in this case.

Adam