在Node.js中从Windows-1251转换为UTF-8 [英] Converting from Windows-1251 to UTF-8 in Node.js

查看:77
本文介绍了在Node.js中从Windows-1251转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个字符串从Windows-1251转换为UTF-8。

I need to convert a string from Windows-1251 to UTF-8.

我尝试用 iconv ,但我得到的是这样的:

I tried to do this with iconv, but all I get is something like this:

пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ

var iconv = new Iconv('windows-1251', 'utf-8')
title = iconv.convert(title).toString('utf-8')


推荐答案

这是解决您问题的方法。您必须先使用Buffer并将字符串转换为二进制。

Here is working solution to your problem. You have to use Buffer and convert your string to binary first.

request({ 
    uri: website_url,
    method: 'GET',
    encoding: 'binary'
}, function (error, response, body) {
        body = new Buffer(body, 'binary');
        conv = new iconv.Iconv('windows-1251', 'utf8');
        body = conv.convert(body).toString();
    }
});

这篇关于在Node.js中从Windows-1251转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆