Gerard Gerard - 4 months ago 59
Node.js Question

JS and text encoding

I have a text file with accented characters like é. I used File Encoding Checker, it appears that the file is encoded as windows-1252.
I read the file with the following JS and node.js code:

fs.readFile('frenchVerbsList.txt','utf-8', function(err, data) {
if (err) {
return console.log("ERROR here!: " + err);
frenchWords = data.split('\r\n');

The output from the console.log statement shows a question mark instead of the accented characters.
What has happened?


Node only supports some encodings and windows-1252 is not part of it. You need to convert the encoding with, for example, encoding to, for example, utf-8.

Similar to this, but haven't tested

var encoding = require("encoding");

fs.readFile('frenchVerbsList.txt', function(err, text) {
    if (err) return console.log("ERROR here!: " + err);

    var resultBuffer = encoding.convert(text, 'utf8', 'windows1252');
    frenchWords = resultBuffer.toString().split('\r\n');