3

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!

4
  • Make sure you write in the same encoding as you read. Commented Jun 6, 2013 at 14:54
  • @TheHippo , if I add 'utf8' or 'ascii' encoding to both, read and write, I get the same and there is no encoding for koi8 there ;( Commented Jun 6, 2013 at 15:02
  • have you tried binary? Why do you need a string? Couldn't this be done with Buffer or streams? Commented Jun 6, 2013 at 15:21
  • @TheHippo Thanks! I tried binary for writeFile and toString and it solved my problem, so you can post it as answer and I will accept it Commented Jun 6, 2013 at 21:43

1 Answer 1

6

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.