S
S
Stanislav2016-02-01 07:09:26
Node.js
Stanislav, 2016-02-01 07:09:26

How to convert encoding from windows-1251 to UTF-8 in node.js?

I don’t understand why it doesn’t work
There is a page with charset=windows-1251
I get text from it using jsdom
�����
I try to convert

var iconv = new Iconv('windows-1251', 'UTF-8');
var x = iconv.convert(text);
console.log(x.toString());

As a result, I get krakozyabry
pїЅpїЅpїЅpїЅpїЅ

What could be the problem? On the page in the header, the encoding is windows-1251.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Artem Kustikov, 2016-02-11
@ms-dred

Here the problem is not with the conversion, but with the output of utf-8 text from the node on Windows: https://github.com/nodejs/node-v0.x-archive/issues/2190
In short: run chcp 65001 in the console before starting the process node.exe

A
alias_eng, 2018-07-28
@alias_eng

I had the same problem when getting html via request and fetch. Found a way out through the needle

N
Nikolai Antonov, 2016-09-17
@my-nickname

How did you decide? Also the same problem.

N
NickKolok, 2017-03-05
@NickKolok

If you need to determine an encoding that is unknown, then for Cyrillic texts here: https://github.com/nickkolok/node-autodetect-utf8-...

N
NyoNor, 2021-07-16
@NyoNor

http.get(url, (stream) => {
            const w = fs.createWriteStream(`./content/${pathToFile}`);
            w.on('finish', () => {
                callback();
            });
            stream
                .pipe(iconv.decodeStream('win1251'))
                .pipe(iconv.encodeStream('utf8'))
                .pipe(w);
});

Works for me... iconv ^0.6.3, node v14.17.1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question