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.
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>
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
'.
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'
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)));
result is:
%a2%cc
%a4%51
even I use Java: System.out.println(URLEncoder.encode("十", "Big5"));
I also get '%A4%51'.
Here is a relevant question:URL Decode Difference between C# and Java
I believe someone might need decode function.lol
Based on @user1783292's answer above, I write the code bellow.
the output is
%A4Q%A4q%A7%DA
, same as Chrome.Maybe there is some standard rule about big5 url encode, but I do not find it. And Java's URLDecoder may also ignore such rules(so it's not correct).
because %51 is char 'Q' in big5, so '%A4Q' is equal to '%A4%51', the urlencode parse it.
what's more, the 'A' in '%A4Q' is case-insensitive, while the 'Q' is not, because 'Q' and 'q' is defferent(%51 and %71)