问题描述
我想使用nodejs将char'''(\ u5341)编码为big5'%A4Q',但我不知道该怎么做。我需要帮助。
I want to use nodejs to encode the char '十'(\u5341) to big5 '%A4Q', but I don't know how to do it. I need help.
更多细节,下面是一个html文件名test.html:
More detail, bellow is a html file names test.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>test</title>
</head>
<body>
<form>
<input name="a"/>
<input type="submit">
</form>
</body>
</html>
在Chrome中打开此文件,输入十并点击提交,即可看到地址栏中的网址是' http://localhost/test.html?a =%A4Q
'。
And open this file in Chrome, type '十' and click 'Submit', you can see the url in the address bar is 'http://localhost/test.html?a=%A4Q
'.
我只想使用nodejs转换与Chrome(和其他浏览器)相同的URL。我尝试使用iconv-lite或node-iconv,但无法将'十'转换为'%A4Q'
I just want to use nodejs to convert url same as Chrome(and other browsers). I tried to use iconv-lite or node-iconv, but can not convert '十' to '%A4Q'
使用iconv-lite和node-iconv我得到了不同的结果。代码是:
Use iconv-lite and node-iconv I got different result. Code is :
var iconv = require('iconv-lite');
var Iconv = require('iconv').Iconv;
var iconv2 = new Iconv('utf8', 'BIG5');
function format(buf) {
var rtn = "";
for(var i=0;i<buf.length;i++) {
rtn += "%" + buf[i].toString(16);
}
return rtn;
}
var chr = '十';
console.log(format(iconv.encode(chr, 'big5')));
console.log(format(iconv2.convert(chr)));
结果是:
%a2%cc
%a4%51
即使我使用Java: System.out.println(URLEncoder.encode(十,Big5));
我也得到'%A4%51'。
even I use Java: System.out.println(URLEncoder.encode("十", "Big5"));
I also get '%A4%51'.
以下是相关问题:
推荐答案
因为%51在big5中是char'Q',所以'% A4Q'等于'%A4%51',urlencode解析它。
because %51 is char 'Q' in big5, so '%A4Q' is equal to '%A4%51', the urlencode parse it.
更重要的是,'%A4Q'中的'A'不区分大小写,而'Q'不是,因为'Q'和'q'是不同的(%51和%71)
what's more, the 'A' in '%A4Q' is case-insensitive, while the 'Q' is not, because 'Q' and 'q' is defferent(%51 and %71)
这篇关于如何在node.js中获取big5 urlencode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!