olivetree123 olivetree123 - 8 months ago 38
Node.js Question

nodejs messy code with Chinese?

var iconv = require('iconv-lite');
var str = 'IGNvbG9yOiAjMjIyOyBwYWRkaW5nLXRvcDogMTc4cHgiPtfwvrS1xLjfvaHPyMn6o6w8YnI+IMT6usOjrCC40NC7xPrRodTx1dDJzNL40NDQxdPDv6ijoTwvcD48L3RkPgogICAgPC90cj4KICAgIDx0’
var bf = new Buffer(str, 'base64');
var html = iconv.decode(bf,'utf8');

With utf8, Chinese can not be shown , English is well. With gb2312 or gbk, it shows nothing , even English can not be shown. What should I do to show it right ?


Seems to work for GB2312 (although my knowledge of Chinese is non-existent so I can't validate):

 color: #222; padding-top: 178px">尊敬的高健先生,<br> 您好, 感谢您选择招商银行信用卡!</p></td>

The above is the result of this:

var html = iconv.decode(bf, 'gb2312');

Of course, your terminal (or whatever you're using to view the output) needs to support UTF-8 for this, because that's what the output will be encoded with.