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 * [Deprecated] 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 ### [Deprecated] Extend Node.js own encodings
59 > NOTE: This doesn't work on latest Node versions. See [details](https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility).
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.12.0).
111 Note: your results may vary, so please always check on your hardware.
113 operation iconv@2.1.4 iconv-lite@0.4.7
114 ----------------------------------------------------------
115 encode('win1251') ~96 Mb/s ~320 Mb/s
116 decode('win1251') ~95 Mb/s ~246 Mb/s
120 * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options
121 (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).
122 A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found.
123 * Encoding: No BOM added, unless overridden by `addBOM: true` option.
127 This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be
128 smart about endianness in the following ways:
129 * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be
130 overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`.
131 * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override.
135 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).
136 Untranslatable characters are set to � or ?. No transliteration is currently supported.
137 Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77).
142 $ git clone git@github.com:ashtuchkin/iconv-lite.git
147 $ # To view performance:
148 $ node test/performance.js
150 $ # To view test coverage:
152 $ open coverage/lcov-report/index.html
156 [![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)
157 [![Codeship Status for ashtuchkin/iconv-lite](https://www.codeship.io/projects/81670840-fa72-0131-4520-4a01a6c01acc/status)](https://www.codeship.io/projects/29053)