Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  132] [ 4]  / answers: 1 / hits: 45385  / 10 Years ago, wed, november 26, 2014, 12:00:00

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(buffer, 'utf8');


It doesn't work. All symbols like å or ä convert to �



How can I save these symbols?


More From » node.js

 Answers
6

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

[#68689] Monday, November 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
jameson questions
;