Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from Windows-1251 to UTF-8 in Node.js

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

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')
like image 693
user1125115 Avatar asked Jan 01 '12 13:01

user1125115


1 Answers

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

const Iconv = require('iconv').Iconv;

request({ 
    uri: website_url,
    method: 'GET',
    encoding: 'binary'
}, function (error, response, body) {

        const body = new Buffer(body, 'binary');
        conv = Iconv('windows-1251', 'utf8');
        body = conv.convert(body).toString();

});
like image 154
sensor Avatar answered Nov 15 '22 13:11

sensor