简体   繁体   English

将字符转换为Javascript中正确的ASCII扩展整数值

[英]Convert character to correct ASCII-extended integer value in Javascript

Code below contains a JS function that receives as parameter String "€€" (Euro sign). 下面的代码包含一个JS函数,该函数接收参数字符串“€€”(欧元符号)。 I need to convert each one of these characters to its ASCII-extended integer equivalent (128 with ISO-8859-1). 我需要将这些字符中的每个字符转换为其等效的ASCII扩展整数(ISO-8859-1为128)。

However, t[0] and t[1] take value "8364" instead of "128". 然而,t [0]和t [1]取值为“ 8364”而不是“ 128”。 What am I doing wrong? 我究竟做错了什么? Please note that if I use UTF-8 instead of ISO-8859-1, they take value 65533 and in the JS debugger (Chrome and IE developer tools) a question mark is displayed instead of € symbol. 请注意,如果我使用UTF-8而不是ISO-8859-1,则它们的值为65533,并且在JS调试器(Chrome和IE开发人员工具)中,显示问号而不是€符号。

Thanks a mil 谢谢你

<html>
<head>
<meta http-equiv="Content-Type" content="text/javascript; charset=ISO-8859-1">
<title>JavaScript Scripting</title>
</head>
<body>
<script type="text/javascript" charset="ISO-8859-1">
function d(s) 
{
    var data = (s + "").split("");
    var dataLength = data.length;
    var t = [dataLength],n;

    for(n=0;n<dataLength;n++)
    t[n]=data[n].charCodeAt(0);
}
d("€€");
</script>
</body>
</html>

Full story is that I pasted this "€€" from file "output.js", where these two bytes were written by Java code below, representing integers [128,128]. 全文是,我从文件“ output.js”中粘贴了此“€€”,这两个字节由下面的Java代码编写,表示整数[128,128]。 That is why I need t[0] and t[1] to get value 128. 这就是为什么我需要t [0]和t [1]来获取值128。

res.setContentType("application/octet-stream"); 
res.setHeader("Content-Disposition","attachment;filename=output.js;charset=ISO-8859-1");
ServletOutputStream os = res.getOutputStream();
char result[]=encode(req.getParameter("originalScript"));
// result[0] and result[1] have here integer value 128
String result2=new String(result);
// result2 is displayed here as non printable characters (blank)
os.print(result2);
// On output.js "€€" is displayed

You have to make sure that the file is actually saved as ISO-8859-1, but that is actually not possible as there is no € character in that character set. 您必须确保文件实际上已保存为ISO-8859-1,但实际上不可能,因为该字符集中没有€字符。

"ISO/IEC 8859-1 is missing [...] the euro sign." “ ISO / IEC 8859-1缺少欧元符号。”

http://en.wikipedia.org/wiki/ISO/IEC_8859-1 http://en.wikipedia.org/wiki/ISO/IEC_8859-1

As you are getting the character code 8364, the file is most likely saved as UTF-8, as that character code corresponds with the unicode character € . 当获得字符代码8364时,该文件很可能保存为UTF-8,因为该字符代码与Unicode字符€相对应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM