Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js toString encoding

I have file encoded with koi8-u

I need to just copy this file, but, through toString()

fs = require('fs')
fs.readFile('fileOne',function(e,data){
    data = data.toString() // now encoding is damaged

    ???  // my code must be here

    fs.writeFile('fileTwo',data)
})

I tried iconv it back using different charsets but with no success. Thanks!

like image 393
igor Avatar asked Feb 13 '26 04:02

igor


1 Answers

You need to write and read everything with binary encoding:

There should be two ways to do this:

Read data as Buffer:

fs = require('fs')
fs.readFile('fileOne', function(e, data){
    // data is a buffer
    buffer = data.toString('binary')


    fs.writeFile('fileTwo', {
        'encoding': 'binary'
    }, buffer);
});

Read data as binary encoded string:

fs = require('fs')
fs.readFile('fileOne', {
        'encoding': 'binary'
    }, function(e, data){
        // data is a string

        fs.writeFile('fileTwo', {
            'encoding': 'binary'
        }, data);
});
like image 130
TheHippo Avatar answered Feb 15 '26 16:02

TheHippo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!