node.js toString encoding

20,537

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);
});
Share:
20,537
igor
Author by

igor

Updated on January 06, 2020

Comments

  • igor
    igor over 4 years

    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!