Skip to content

Instantly share code, notes, and snippets.

@nestorlafon
Created September 22, 2010 11:44
Show Gist options
  • Save nestorlafon/591543 to your computer and use it in GitHub Desktop.
Save nestorlafon/591543 to your computer and use it in GitHub Desktop.
$ file ISO-8859-1.txt
ISO-8859-1.txt: ISO-8859 text
$ node iconvtest.js
DEBUG: Raw data from ISO file: Este es un texto en español donde tenemos acentos en la á y en otras vocales como la é,í,ó,ú. También hay otros símbolos pero con esto vale.
DEBUG: New input 10485760 bytes buffer created
DEBUG: 141 bytes written to iBuf in binary
DEBUG: siBuf size: 141
DEBUG: siBuf contents: Este es un texto en español donde tenemos acentos en la á y en otras vocales como la é,í,ó,ú. También hay otros símbolos pero con esto vale.
DEBUG: oBuf size: 149
DEBUG: oBuf contents: Este es un texto en español donde tenemos acentos en la á y en otras vocales como la é,í,ó,ú. También hay otros símbolos pero con esto vale.
It's saved!
$ file converted.txt
converted.txt: UTF-8 Unicode text
$ cat converted.txt
Este es un texto en español donde tenemos acentos en la á y en otras vocales como la é,í,ó,ú. También hay otros símbolos pero con esto vale.
$ cat iconvtest.js
var sys = require('sys');
var fs = require('fs');
var Iconv = require('iconv').Iconv;
var Buffer = require('buffer').Buffer;
var bufSize = 10485760;
fs.readFile('ISO-8859-1.txt', 'binary', function (err, data) {
if (err) throw err;
sys.debug("Raw data from ISO file: " + data);
var iBuf = new Buffer(bufSize);
sys.debug("New input " + bufSize + " bytes buffer created");
var wSize = iBuf.write(data, 0, 'binary');
sys.debug( wSize + " bytes written to iBuf in binary");
var siBuf = iBuf.slice(0, wSize);
sys.debug("siBuf size: " + siBuf.length);
sys.debug("siBuf contents: " + siBuf.toString('binary'));
var iconv = new Iconv('ISO-8859-1', 'UTF-8');
var oBuf = iconv.convert(siBuf);
sys.debug("oBuf size: " + oBuf.length);
sys.debug("oBuf contents: " + oBuf.toString('utf8') );
fs.writeFile('converted.txt', oBuf.toString('utf8') , function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment