search

Home  >  Q&A  >  body text

node.js - nodejs如何把utf-8的字符转化为gb2312

我需要往一个网站 post一组数据 ,目标网站只接收gb2312编码的。其它接到是乱码。
nodejs如何把utf-8的字符转化为gb2312?



var iconv = require('iconv-lite');
var str = '你好';
str = iconv.encode(str,'utf8');
str = iconv.decode(str,'GB2312');
str = str.toString();
console.log(str); 

这样是不行的。求指导

天蓬老师天蓬老师2786 days ago666

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:38:13

    I have encountered this before, you can take a look at this http://web-engineer.cn/article/29

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:38:13

    The first floor is great, superAgent-charset realizes the function! ! !
    It’s a pity that my reputation cannot reach 15

    I made a corresponding summary, you can see: Link description

    Using NodeJs to implement news Rss crawler

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:38:13

    iconv.encode(testString, 'utf8').toString('gb2312')

    https://github.com/ashtuchkin...

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:38:13

    var iconv = require('iconv-lite');

    ....

    body = iconv.decode(body, 'gbk');

    reply
    0
  • Cancelreply