Convert iso-8859-1 to utf-8 javascript

I am trying to analyze the page "iso-8859-1" and save it in my db using utf-8, this is my code:

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

This does not work. All characters, such as å or ä, are converted to �

How to save these characters?

+6
source share
2 answers

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

  • Open the input file in binary mode, so JavaScript does not assume UTF-8 and does not try to convert it to internal encoding:

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

     var iconv = require('iconv-lite'); var output = iconv.decode(input, "ISO-8859-1"); 
  • Save buffer for file output:

     fs.writeFileSync(outputFilePath, output); 

If you are unsure of the encoding names, you can check if this encoding is supported with encodingExists() :

 > iconv.encodingExists("ISO-8859-1"); true 
+15
source

This works 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); 

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

0
source

Source: https://habr.com/ru/post/978747/


All Articles