1 ## Pure JS character encoding conversion
3 <!-- [![Build Status](https://secure.travis-ci.org/ashtuchkin/iconv-lite.png?branch=master)](http://travis-ci.org/ashtuchkin/iconv-lite) -->
5 * Doesn't need native code compilation. Works on Windows and in sandboxed environments like [Cloud9](http://c9.io).
6 * Used in popular projects like [Express.js (body_parser)](https://github.com/expressjs/body-parser),
7 [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.
8 * Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).
9 * Intuitive encode/decode API
10 * Streaming support for Node v0.10+
11 * Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
12 * In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).
15 [![NPM Stats](https://nodei.co/npm/iconv-lite.png?downloads=true)](https://npmjs.org/packages/iconv-lite/)
20 var iconv = require('iconv-lite');
22 // Convert from an encoded buffer to js string.
23 str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
25 // Convert from js string to an encoded buffer.
26 buf = iconv.encode("Sample input string", 'win1251');
28 // Check if encoding is supported
29 iconv.encodingExists("us-ascii")
32 ### Streaming API (Node v0.10+)
35 // Decode stream (from binary stream to js strings)
36 http.createServer(function(req, res) {
37 var converterStream = iconv.decodeStream('win1251');
38 req.pipe(converterStream);
40 converterStream.on('data', function(str) {
41 console.log(str); // Do something with decoded strings, chunk-by-chunk.
45 // Convert encoding streaming example
46 fs.createReadStream('file-in-win1251.txt')
47 .pipe(iconv.decodeStream('win1251'))
48 .pipe(iconv.encodeStream('ucs2'))
49 .pipe(fs.createWriteStream('file-in-ucs2.txt'));
51 // Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
52 http.createServer(function(req, res) {
53 req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
54 assert(typeof body == 'string');
55 console.log(body); // full request body string
60 ### Extend Node.js own encodings
62 // After this call all Node basic primitives will understand iconv-lite encodings.
63 iconv.extendNodeEncodings();
66 buf = new Buffer(str, 'win1251');
67 buf.write(str, 'gbk');
68 str = buf.toString('latin1');
69 assert(Buffer.isEncoding('iso-8859-15'));
70 Buffer.byteLength(str, 'us-ascii');
72 http.createServer(function(req, res) {
73 req.setEncoding('big5');
74 req.collect(function(err, body) {
79 fs.createReadStream("file.txt", "shift_jis");
81 // External modules are also supported (if they use Node primitives, which they probably do).
82 request = require('request');
84 url: "http://github.com/",
88 // To remove extensions
89 iconv.undoExtendNodeEncodings();
92 ## Supported encodings
94 * All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.
95 * Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap.
96 * All widespread singlebyte encodings: Windows 125x family, ISO-8859 family,
97 IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library.
98 Aliases like 'latin1', 'us-ascii' also supported.
99 * All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.
101 See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).
103 Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!
105 Multibyte encodings are generated from [Unicode.org mappings](http://www.unicode.org/Public/MAPPINGS/) and [WHATWG Encoding Standard mappings](http://encoding.spec.whatwg.org/). Thank you, respective authors!
108 ## Encoding/decoding speed
110 Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.10.26).
111 Note: your results may vary, so please always check on your hardware.
113 operation iconv@2.1.4 iconv-lite@0.4.0
114 ----------------------------------------------------------
115 encode('win1251') ~130 Mb/s ~380 Mb/s
116 decode('win1251') ~127 Mb/s ~210 Mb/s
121 When decoding, be sure to supply a Buffer to decode() method, otherwise [bad things usually happen](https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding).
122 Untranslatable characters are set to � or ?. No transliteration is currently supported.
123 Uses BOM to determine endianness, but doesn't remove it. Use ['strip-bom' module](https://github.com/sindresorhus/strip-bom).
124 Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77).
129 $ git clone git@github.com:ashtuchkin/iconv-lite.git
134 $ # To view performance:
135 $ node test/performance.js
137 $ # To view test coverage:
139 $ open coverage/lcov-report/index.html
143 [![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)
144 [![Codeship Status for ashtuchkin/iconv-lite](https://www.codeship.io/projects/81670840-fa72-0131-4520-4a01a6c01acc/status)](https://www.codeship.io/projects/29053)