How to convert character encoding from CP932 to UTF-8 in nodejs javascript, using the nodejs-iconv module (or other solution)

后端 未结 3 921
天涯浪人
天涯浪人 2020-12-15 14:07

I\'m attempting to convert a string from CP932 (aka Windows-31J) to utf8 in javascript. Basically I\'m crawling a site that ignores the utf-8 request in the request header

相关标签:
3条回答
  • 2020-12-15 14:32

    I got same trouble today :)
    It depends libiconv. You need libiconv-1.13-ja-1.patch.
    Please check followings.

    • http://d.hatena.ne.jp/ushiboy/20110422/1303481470
    • http://code.xenophy.com/?p=1529

    or you can avoid problem using iconv-jp try

    npm install iconv-jp
    0 讨论(0)
  • 2020-12-15 14:34

    I had same problem, but with CP1250. I was looking for problem everywhere and everything was OK, except call of request – I had to add encoding: 'binary'.

    request = require('request')
    Iconv  = require('iconv').Iconv
    
    request({uri: url, encoding: 'binary'}, function(err, response, body) {
        body = new Buffer(body, 'binary')
        iconv = new Iconv('CP1250', 'UTF8')
        body = iconv.convert(body).toString()
        // ...
    })
    
    0 讨论(0)
  • 2020-12-15 14:39

    https://github.com/bnoordhuis/node-iconv/issues/19

    I tried /Users/Me/node_modules/iconv/test.js node test.js. It return error.

    On Mac OS X Lion, this problem seems depend on gcc.

    0 讨论(0)
提交回复
热议问题