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 [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.
7
* Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).
8
* Intuitive encode/decode API
9
* Streaming support for Node v0.10+
10
* Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
11
* In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).
14
[![NPM Stats](https://nodei.co/npm/iconv-lite.png?downloads=true)](https://npmjs.org/packages/iconv-lite/)
19
var iconv = require('iconv-lite');
21
// Convert from an encoded buffer to js string.
22
str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
24
// Convert from js string to an encoded buffer.
25
buf = iconv.encode("Sample input string", 'win1251');
27
// Check if encoding is supported
28
iconv.encodingExists("us-ascii")
31
### Streaming API (Node v0.10+)
34
// Decode stream (from binary stream to js strings)
35
http.createServer(function(req, res) {
36
var converterStream = iconv.decodeStream('win1251');
37
req.pipe(converterStream);
39
converterStream.on('data', function(str) {
40
console.log(str); // Do something with decoded strings, chunk-by-chunk.
44
// Convert encoding streaming example
45
fs.createReadStream('file-in-win1251.txt')
46
.pipe(iconv.decodeStream('win1251'))
47
.pipe(iconv.encodeStream('ucs2'))
48
.pipe(fs.createWriteStream('file-in-ucs2.txt'));
50
// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
51
http.createServer(function(req, res) {
52
req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
53
assert(typeof body == 'string');
54
console.log(body); // full request body string
59
### Extend Node.js own encodings
61
// After this call all Node basic primitives will understand iconv-lite encodings.
62
iconv.extendNodeEncodings();
65
buf = new Buffer(str, 'win1251');
66
buf.write(str, 'gbk');
67
str = buf.toString('latin1');
68
assert(Buffer.isEncoding('iso-8859-15'));
69
Buffer.byteLength(str, 'us-ascii');
71
http.createServer(function(req, res) {
72
req.setEncoding('big5');
73
req.collect(function(err, body) {
78
fs.createReadStream("file.txt", "shift_jis");
80
// External modules are also supported (if they use Node primitives, which they probably do).
81
request = require('request');
83
url: "http://github.com/",
87
// To remove extensions
88
iconv.undoExtendNodeEncodings();
91
## Supported encodings
93
* All node.js native encodings: utf8, ucs2 / utf16, ascii, binary, base64, hex.
94
* Additional unicode encodings: utf16, utf16-be.
95
* All widespread singlebyte encodings: Windows 125x family, ISO-8859 family,
96
IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library.
97
Aliases like 'latin1', 'us-ascii' also supported.
98
* All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.
100
See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).
102
Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!
104
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!
107
## Encoding/decoding speed
109
Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.10.26).
110
Note: your results may vary, so please always check on your hardware.
112
operation iconv@2.1.4 iconv-lite@0.4.0
113
----------------------------------------------------------
114
encode('win1251') ~130 Mb/s ~380 Mb/s
115
decode('win1251') ~127 Mb/s ~210 Mb/s
120
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).
121
Untranslatable characters are set to � or ?. No transliteration is currently supported.
126
$ git clone git@github.com:ashtuchkin/iconv-lite.git
131
$ # To view performance:
132
$ node test/performance.js
136
[![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)