iconv-lite

Convert character encodings in pure javascript.

  • Doesn't need native code compilation. Works on Windows and in sandboxed environments like Cloud9.
  • Used in popular projects like Express.js (body_parser), Grunt, Nodemailer, Yeoman and others.
  • Faster than node-iconv (see below for performance comparison).
  • Intuitive encode/decode API
  • Streaming support for Node v0.10+
  • Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
  • In-browser usage via Browserify (~180k gzip compressed with Buffer shim included).
  • License: MIT.

var iconv = require('iconv-lite');
 
// Convert from an encoded buffer to js string. 
str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
 
// Convert from js string to an encoded buffer. 
buf = iconv.encode("Sample input string", 'win1251');
 
// Check if encoding is supported 
iconv.encodingExists("us-ascii")
 
// Decode stream (from binary stream to js strings) 
http.createServer(function(reqres) {
    var converterStream = iconv.decodeStream('win1251');
    req.pipe(converterStream);
 
    converterStream.on('data', function(str) {
        console.log(str); // Do something with decoded strings, chunk-by-chunk. 
    });
});
 
// Convert encoding streaming example 
fs.createReadStream('file-in-win1251.txt')
    .pipe(iconv.decodeStream('win1251'))
    .pipe(iconv.encodeStream('ucs2'))
    .pipe(fs.createWriteStream('file-in-ucs2.txt'));
 
// Sugar: all encode/decode streams have .collect(cb) method to accumulate data. 
http.createServer(function(reqres) {
    req.pipe(iconv.decodeStream('win1251')).collect(function(errbody) {
        assert(typeof body == 'string');
        console.log(body); // full request body string 
    });
});
// After this call all Node basic primitives will understand iconv-lite encodings. 
iconv.extendNodeEncodings();
 
// Examples: 
buf = new Buffer(str, 'win1251');
buf.write(str, 'gbk');
str = buf.toString('latin1');
assert(Buffer.isEncoding('iso-8859-15'));
Buffer.byteLength(str, 'us-ascii');
 
http.createServer(function(reqres) {
    req.setEncoding('big5');
    req.collect(function(errbody) {
        console.log(body);
    });
});
 
fs.createReadStream("file.txt", "shift_jis");
 
// External modules are also supported (if they use Node primitives, which they probably do). 
request = require('request');
request({
    url: "http://github.com/", 
    encoding: "cp932"
});
 
// To remove extensions 
iconv.undoExtendNodeEncodings();
  • All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.
  • Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap.
  • All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. Aliases like 'latin1', 'us-ascii' also supported.
  • All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.

See all supported encodings on wiki.

Most singlebyte encodings are generated automatically from node-iconv. Thank you Ben Noordhuis and libiconv authors!

Multibyte encodings are generated from Unicode.org mappings and WHATWG Encoding Standard mappings. Thank you, respective authors!

Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.10.26). Note: your results may vary, so please always check on your hardware.

operation             iconv@2.1.4   iconv-lite@0.4.0
----------------------------------------------------------
encode('win1251')     ~130 Mb/s     ~380 Mb/s
decode('win1251')     ~127 Mb/s     ~210 Mb/s

When decoding, be sure to supply a Buffer to decode() method, otherwise bad things usually happen.
Untranslatable characters are set to � or ?. No transliteration is currently supported.
Uses BOM to determine endianness, but doesn't remove it. Use 'strip-bom' module.
Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77).

$ git clone git@github.com:ashtuchkin/iconv-lite.git
$ cd iconv-lite
$ npm install
$ npm test
    
$ # To view performance:
$ node test/performance.js
 
$ # To view test coverage:
$ npm run coverage
$ open coverage/lcov-report/index.html