具有ASCII> 128在Javascript中没有正确读取 [英] Characters with ASCII > 128 are not correctly read in Javascript
问题描述
我有一个包含Javascript文件的HTML。这个脚本包含一个特殊字符,ASCII 152.当我尝试显示charCodeAt,我得到不同的结果,但从来没有正确的。你能请教吗?感谢
TEST.HTML
< script type =text / javascriptcharset = SEE BELOW src =test.js>
< / script>
使用ANSI编码的TEST.JS文件
函数d(a)
{
a =(a +)。
alert(a [1] .charCodeAt(0));
};
d(i〜g); // Note that〜is 152 in ASCII
- TEST.HTML with x用户定义的字符集:alert显示63384.使用%63232工作,因为每个字符> 128显示为63232 +字符。
- TEST.HTML与utf-8字符集:alert显示65533
- TEST.HTML与Windows-1252字符集:alert显示752.我找不到ASCII和显示内容之间的关系。
使用UTF-8编码的TEST.JS文件
function d(a)
{
a =(a +)。
alert(a [1] .charCodeAt(0));
};
d(i [x98] g); //注意x98是152
- TEST.HTML with x-user
- 使用utf-8字符集的TEST.HTML:alert显示65533.所有字符> 128显示为65533。显示65533.所有字符> 128显示为65533。
- TEST.HTML与Windows-1252字符集:alert显示65533.所有字符> 128显示为65533.
<对于utf8,范围128-255中没有字符,ASCII完全在127结束...还有位置
1 <=>的字符 / code>在i [x98] g
是[
,<$ c
您的函数可以替换为 str.charCodeAt(1) )
。
字符〜
是
Unicode字符SMALL TILDE(U + 02DC ,可写为\\\˜
或 String.fromCharCode(732)
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.
解决方案 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)
这篇关于具有ASCII> 128在Javascript中没有正确读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!