Reputation: 1319
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?
Upvotes: 18
Views: 50977
Reputation: 1
I want to identify the file on the picture enter image description here
The following code can make Chinese not garbled
import iconv from 'iconv-lite';
var buffers = fs.readFileSync(path.join(__dirname, 'file/1.csv'));
var output = iconv.decode(buffers, 'gbk');
Upvotes: 0
Reputation: 3368
You can now decode strings using the web standard TextDecoder
(works in node & deno too):
const { readFile } from 'fs'
const encoded = await readFile(inputFilePath)
const decoded = new TextDecoder('windows-1252').decode(encoded)
note that windows-1252
is equivalent to ISO-8859-1
for more, checkout https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
Upvotes: 1
Reputation: 4162
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
Upvotes: 4
Reputation: 146340
You need a third-party library for that task. You are using iconv-lite so you need to follow these steps:
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"});
Convert from ISO-8859-1 to Buffer:
var iconv = require('iconv-lite');
var output = iconv.decode(input, "ISO-8859-1");
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
Upvotes: 34