javascriptnode.jsurlencodebig5

how to get big5 urlencode in node.js?


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


Solution

  • 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)