Convert iso-8859-1 to utf-8 javascript

前端 未结 2 1169
既然无缘
既然无缘 2020-12-31 06:39

I try to parse a \"iso-8859-1\" page and save to my DB with utf-8, this is my code:

var buffer = iconv.encode(data, \"iso-8859-1\");
data = iconv.decode(buff         


        
相关标签:
2条回答
  • 2020-12-31 07:13

    It's working for me:

    var tempBuffer = new Buffer(response.body, 'iso-8859-1');
    var iconv = new Iconv('ISO-8859-1', 'UTF-8');
    var tempBuffer = iconv.convert(tempBuffer);
    

    used 'iconv' module https://github.com/bnoordhuis/node-iconv

    0 讨论(0)
  • 2020-12-31 07:14

    You need a third-party library for that task. You are using iconv-lite so you need to follow these steps:

    1. Open input file in binary mode, so JavaScript doesn't assume UTF-8 nor try to convert to its internal encoding:

      var fs = require("fs");
      var input = fs.readFileSync(inputFilePath, {encoding: "binary"});
      
    2. Convert from ISO-8859-1 to Buffer:

      var iconv = require('iconv-lite');
      var output = iconv.decode(input, "ISO-8859-1");
      
    3. Save Buffer to output file:

      fs.writeFileSync(outputFilePath, output);
      

    If unsure of encoding names, you can test whether a given encoding is supported with encodingExists():

    > iconv.encodingExists("ISO-8859-1");
    true
    
    0 讨论(0)
提交回复
热议问题