Converting from Windows-1255 to UTF-8 in Node JS

2019-08-10 20:13发布

I'm extracting text from a Windows-1255-encoded webpage using Node.js. I'm trying to decode the text using the following Windows-1255 encoder/decoder: https://www.npmjs.com/package/windows-1255

After installing it using NPM and requiring it in the relevant file, I tried using it like this:

var title = windows1255.decode('#title').text());

This doesn't seem to have any effect. Any ideas as to why?

Thanks!

Maor

1条回答
虎瘦雄心在
2楼-- · 2019-08-10 20:52

don't know if you still waiting for an answer about this issue, but the following worked for me...

When fetching the data (a file), I set the get options of encoding to be binary:

var options = {
        method: 'GET',
        url: 'myURL',
        encoding: 'binary' 
    };

    request(options, function (error, response, body) {

        //deal with hebrew encoding
        csvString = encoding.convert(body, 'UTF8', "CP1255").toString();

Then for I switch encoding from CP1255 (=windows1255) to UTF8.

Hope it helps :)

查看更多
登录 后发表回答