javascriptnode.jsencodingpolish

Read from file and write to file 'Ł' character with nodejs


I have an ansi file with 'Ł' character I would like to read this character and save it to another file with the same encoding(to get 'Ł' character).

const fs = require('fs');
var content = fs.readFileSync('input.txt',null); //Ł
fs.writeFileSync('output.txt',content,null); //Ł how to get this?

How to do this with nodejs?

Update: I checked the input file:

file -i

text/plain charset=uknown-8bit

file -r

Non-ISO extended-ASCII, text with very long lines, with CRLF line terminators

Can I save the file with such details?


Solution

  • I achieved what I wanted by creating binary strings out of buffer with the problematic characters:

    const fs = require('fs');
    function getChar(charId) {
        let characters = { AA:165, //Ą
             EE:202, //Ę
             CC:198, //Ć
             LL:163, //Ł
             SS:140, //Ś
             OO:211, //Ó
             ZZ:175, //Ż
             ZZZ:143, //Ź
             aa:185, //ą
             ee:234, //ę
             cc:230, //ć
             ll:179, //ł
             ss:156, //ś
             oo:243, //ó
             zz:191, //ź
             zzz:159 //ź
        };
        let buf = new Buffer({
            type:'Buffer',
            data:[ characters[charId] ],
            encoding:'ISO-8859-2'
        });
        return buf.toString('binary')
    }
    fs.writeFileSync('output.txt',getChar('LL')+" test",'ascii'); //'Ł test'