1 ## Pure JS character encoding conversion [![Build Status](https://travis-ci.org/ashtuchkin/iconv-lite.svg?branch=master)](https://travis-ci.org/ashtuchkin/iconv-lite)
3 * Doesn't need native code compilation. Works on Windows and in sandboxed environments like [Cloud9](http://c9.io).
4 * Used in popular projects like [Express.js (body_parser)](https://github.com/expressjs/body-parser),
5 [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.
6 * Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).
7 * Intuitive encode/decode API
8 * Streaming support for Node v0.10+
9 * Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
10 * In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).
13 [![NPM Stats](https://nodei.co/npm/iconv-lite.png?downloads=true&downloadRank=true)](https://npmjs.org/packages/iconv-lite/)
18 var iconv = require('iconv-lite');
20 // Convert from an encoded buffer to js string.
21 str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
23 // Convert from js string to an encoded buffer.
24 buf = iconv.encode("Sample input string", 'win1251');
26 // Check if encoding is supported
27 iconv.encodingExists("us-ascii")
30 ### Streaming API (Node v0.10+)
33 // Decode stream (from binary stream to js strings)
34 http.createServer(function(req, res) {
35 var converterStream = iconv.decodeStream('win1251');
36 req.pipe(converterStream);
38 converterStream.on('data', function(str) {
39 console.log(str); // Do something with decoded strings, chunk-by-chunk.
43 // Convert encoding streaming example
44 fs.createReadStream('file-in-win1251.txt')
45 .pipe(iconv.decodeStream('win1251'))
46 .pipe(iconv.encodeStream('ucs2'))
47 .pipe(fs.createWriteStream('file-in-ucs2.txt'));
49 // Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
50 http.createServer(function(req, res) {
51 req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
52 assert(typeof body == 'string');
53 console.log(body); // full request body string
58 ### Extend Node.js own encodings
60 // After this call all Node basic primitives will understand iconv-lite encodings.
61 iconv.extendNodeEncodings();
64 buf = new Buffer(str, 'win1251');
65 buf.write(str, 'gbk');
66 str = buf.toString('latin1');
67 assert(Buffer.isEncoding('iso-8859-15'));
68 Buffer.byteLength(str, 'us-ascii');
70 http.createServer(function(req, res) {
71 req.setEncoding('big5');
72 req.collect(function(err, body) {
77 fs.createReadStream("file.txt", "shift_jis");
79 // External modules are also supported (if they use Node primitives, which they probably do).
80 request = require('request');
82 url: "http://github.com/",
86 // To remove extensions
87 iconv.undoExtendNodeEncodings();
90 ## Supported encodings
92 * All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.
93 * Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap.
94 * All widespread singlebyte encodings: Windows 125x family, ISO-8859 family,
95 IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library.
96 Aliases like 'latin1', 'us-ascii' also supported.
97 * All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.
99 See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).
101 Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!
103 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!
106 ## Encoding/decoding speed
108 Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.12.0).
109 Note: your results may vary, so please always check on your hardware.
111 operation iconv@2.1.4 iconv-lite@0.4.7
112 ----------------------------------------------------------
113 encode('win1251') ~96 Mb/s ~320 Mb/s
114 decode('win1251') ~95 Mb/s ~246 Mb/s
118 * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options
119 (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).
120 A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found.
121 * Encoding: No BOM added, unless overridden by `addBOM: true` option.
125 This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be
126 smart about endianness in the following ways:
127 * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be
128 overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`.
129 * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override.
133 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).
134 Untranslatable characters are set to � or ?. No transliteration is currently supported.
135 Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77).
140 $ git clone git@github.com:ashtuchkin/iconv-lite.git
145 $ # To view performance:
146 $ node test/performance.js
148 $ # To view test coverage:
150 $ open coverage/lcov-report/index.html
154 [![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)
155 [![Codeship Status for ashtuchkin/iconv-lite](https://www.codeship.io/projects/81670840-fa72-0131-4520-4a01a6c01acc/status)](https://www.codeship.io/projects/29053)