4

I have a HTML that includes a Javascript file. This script contains a special character, ASCII 152. When I try to display the charCodeAt, I get different results, but never the right one. Could you please advice? Thanks

TEST.HTML

<script type="text/javascript" charset=SEE BELOW src="test.js">
</script>

TEST.JS file with ANSI encoding

function d(a)
{
a=(a+"").split("");
alert(a[1].charCodeAt(0));
};
d("i˜g"); // Note that ˜ is 152 in ASCII
  • TEST.HTML with x-user-defined charset: alert shows 63384. With %63232 works, as every char >128 is displayed as 63232+char.
  • TEST.HTML with utf-8 charset: alert shows 65533. All chars > 128 are displayed as 65533.
  • TEST.HTML with Windows-1252 charset: alert shows 752. I cannot find a relation between ASCII and what is displayed.

TEST.JS file with UTF-8 encoding

function d(a)
{
a=(a+"").split("");
alert(a[1].charCodeAt(0));
};
d("i[x98]g"); // Note that x98 is 152
  • TEST.HTML with x-user-defined charset: alert shows 65533. All chars > 128 are displayed as 65533.
  • TEST.HTML with utf-8 charset: alert shows 65533. All chars > 128 are displayed as 65533.
  • TEST.HTML with Windows-1252 charset: alert shows 65533. All chars > 128 are displayed as 65533.
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

2 Answers2

6

There is no characters in range 128-255 for utf8, and ASCII ends completely at 127... Also the character at position 1 in "i[x98]g" is a "[", the "[x98]" is meaningless.

Your function can be replaced with str.charCodeAt(1).

The character ˜ is Unicode Character 'SMALL TILDE' (U+02DC and can be written as "\u02DC", or String.fromCharCode(732)

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 1
    Additional info: [charCodeAt()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/charCodeAt) works with Unicode and does not really care about ASCII. – Álvaro González Apr 09 '12 at 09:01
  • Sorry, I meant ASCII EXTENDED. As per below, 152 is '˜' http://www.ascii-code.com/ [x98] is one character. It is the way Notepad++ represents that 152 char in UTF-8. 98 in hexadecimal is 152. –  Apr 09 '12 at 10:12
  • Thank you all. Then I rephrase my question. I want to write a character representing integer 152 in TEST.JS as parameter of d(), so that when I run this function alert displays 152. I am open to use any encoding. –  Apr 09 '12 at 10:16
  • 1
    @Arturo in that case you can to write `"\u0098"`, or this invisible character: `""`, or `String.fromCharCode(152)` – Esailija Apr 09 '12 at 10:23
  • @Arturo, that’s a completely new question and should be asked separately. I cannot make sense of that question either, as there is no character representing integer 152. If you wish to map characters to integers according to a scheme of your own, or applying someone else’s mapping, use table for that. But it is difficult to guess what might be the real question here. – Jukka K. Korpela Apr 09 '12 at 11:34
  • I believe there is some kind of problem between chars 152 (\u0098) and 732 (\u02dc), as both are displayed with the same symbol. How can I force charCodeAt() to use the correct one (152)? http://www.fileformat.info/info/unicode/char/2dc/index.htm http://www.fileformat.info/info/unicode/char/098/index.htm –  Apr 09 '12 at 21:31
  • Thank you all. As suggested, I have opened a new question: http://stackoverflow.com/questions/10080605/special-character-u0098-read-as-u02dc-using-charcodeat –  Apr 09 '12 at 22:20
0

ASCII only has 127 characters. char 152 is non-existant

hofnarwillie
  • 3,563
  • 10
  • 49
  • 73
  • 1
    Sorry, I meant ASCII EXTENDED. As per below, 152 is '˜' http://www.ascii-code.com/ –  Apr 09 '12 at 10:08