1

I've been trying to convert from utf8 string to windows-1255 string for along time now. I came up with this code:

var fs = require('fs');
var Iconv = require('iconv').Iconv;
var iconvlite = require('iconv-lite');
var windows1255 = require('windows-1255');

/////////////////// Attempting to converted from utf-8 to windows-1255
function encode(content) {
  var iconv = new Iconv('UTF-8', 'CP1255//TRANSLIT//IGNORE');
  var buffer2 = iconv.convert(content);
  return buffer2;
  //return buffer.toString('ISO-88591-1');      // toString cannot convert in the encoding windows1255
};
var buffer3 = new Buffer(encode(fs.readFileSync('/home/USER/git/npm-test-1/utf8.txt')));
console.log(buffer3);
//str = iconvlite.decode(buffer3, 'win1255');
str = iconvlite.decode(buffer3, "1255");
console.log(str);


fs.writeFile("/home/USER/git/npm-test-1/windows-1255.txt", str, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

Only problem is on the last step, when I'm trying to convert from a buffer to a string. It converts it alright but to a utf8 encoding string :|

  1. Is this a bug?

  2. Is there any way around this? I really need windows-1255 string and not a buffer.

  3. Anyone knows a way to do this?
liron t
  • 11
  • 1
  • Don't decode your buffer, just write it. If you really need to use a string type to perform your write, use `String.fromCharCode` to create a string with the binary data of your _windows-1255_ blob – Paul S. Nov 19 '17 at 02:35
  • @PaulS. Thanks for trying to help me out. But this method didn't work. I got `c3a0` on `xxd windows-1255.txt` when i should have got `e0a0` – liron t Nov 19 '17 at 10:09

0 Answers0