ASCII > 字符128 未在 Javascript 中正确读取 [英] Characters with ASCII > 128 are not correctly read in Javascript
问题描述
我有一个包含 Javascript 文件的 HTML.该脚本包含一个特殊字符 ASCII 152.当我尝试显示 charCodeAt 时,我得到了不同的结果,但从来没有正确的结果.你能指点一下吗?谢谢
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>
ANSI 编码的 TEST.JS 文件
function d(a)
{
a=(a+"").split("");
alert(a[1].charCodeAt(0));
};
d("i˜g"); // Note that ˜ is 152 in ASCII
- 带有 x 用户定义字符集的 TEST.HTML:警报显示 63384.使用 %63232 可以工作,因为每个 >128 的字符都显示为 63232+字符.
- 带有 utf-8 字符集的 TEST.HTML:警报显示 65533.所有大于 128 的字符都显示为 65533.
- 使用 Windows-1252 字符集的 TEST.HTML:警报显示 752.我找不到 ASCII 和显示内容之间的关系.
使用 UTF-8 编码的 TEST.JS 文件
function d(a)
{
a=(a+"").split("");
alert(a[1].charCodeAt(0));
};
d("i[x98]g"); // Note that x98 is 152
- 带有 x 用户定义字符集的 TEST.HTML:警报显示 65533.所有大于 128 的字符都显示为 65533.
- 带有 utf-8 字符集的 TEST.HTML:警报显示 65533.所有大于 128 的字符都显示为 65533.
- 使用 Windows-1252 字符集的 TEST.HTML:警报显示 65533.所有大于 128 的字符都显示为 65533.
推荐答案
utf8 没有 128-255 范围内的字符,ASCII 完全在 127 处结束...也是1
在"i[x98]g"
中是一个"["
,"[x98]"
是没有意义的.
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.
您的函数可以替换为 str.charCodeAt(1)
.
Your function can be replaced with str.charCodeAt(1)
.
字符~
是Unicode 字符 'SMALL TILDE' (U+02DC 并且可以写成 "u02DC"
或 String.fromCharCode(732)
The character ˜
is
Unicode Character 'SMALL TILDE' (U+02DC and can be written as "u02DC"
, or String.fromCharCode(732)
这篇关于ASCII > 字符128 未在 Javascript 中正确读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!