Node. js encoding and conversion

Source: Internet
Author: User

The toString () method provided by Node. js does not support gbk. Therefore, a third-party library needs to be loaded during Chinese conversion. We recommend the following two encoding conversion libraries: iconv-lite and encoding. What are iconv, iconv-lite, and encoding? Iconv: a standard character set conversion interface in Unix-like systems, used for conversion between different character set encoding, first appeared in the HP-UX system. Iconv-lite: it is a pure js Implementation of iconv. Supported encoding includes node. js native encoding: utf8, ucs2, ascii, binary, base64; also supports a wide range of single-byte encoding: Windows 125x family, ISO-8859 family, IBM/DOS codepages, Macintosh family, KOI8 family, latin1, us-ascii; multi-byte encoding: gbk, gb2313, Big5, cp950. Officially announced faster than node-iconv. Encoding: Re-encapsulate node-iconv and iconv-lite. encoding first calls node-iconv. If node-iconv cannot be parsed, call iconv-lite as an alternative. Iconv-lite installation and use 1. install $ npm install iconv-lite 2. use var iconv = require ('iconv-lite '); // Convert from an encoded buffer to js string. str = iconv. decode (buf, 'win1251 '); // Convert from js string to an encoded buffer. buf = iconv. encode ("Sample input string", 'win1251 '); // Check if encoding is supportediconv. encodingExists ("us-ascii") Copy code 3. performance Comparison Between iconv-lite and node-iconv operation iconv@1.2.4 iconv-l Ite@0.2.4 ---------------------------------------------------------- encode ('win1251 ')~ 115 Mb/s ~ 230 Mb/sdecode ('win1251 ')~ 95 Mb/s ~ 130 Mb/s encoding installation use 1. install $ npm install encoding 2. Use the encoding module as a convert () method. Use encoding. convert (text, toCharset, fromCharset ). Text: the object to be converted. It can be a Buffer or String object. ToCharset: The converted encoding. FromCharset: encoding before conversion. The default value is uft8. The converted input result is a Buffer object. Var encoding = require ('encoding'); var result = encoding. convert ("Ä äöü", "Latin_1"); console. log (result); // <Buffer d5 c4 d6 dc> outside: jschardet what is jschardet? Jschardet is the jschardet that is ported from chardet of Python to Javascript by Ant ónio Afonso. 1. install $ npm install jschardet 2. use var jschardet = require ("jschardet ") // "maid" in UTF-8jschardet.detect ("\ xc3 \ xa0 \ xc3 \ xad \ xc3 \ xa0 \ xc3 \ xa7 \ xc3 \ xa3") // {encoding: "UTF-8", confidence: 0.9690625} // "frequently used Chinese Character Base table" in Big5 jschardet. detect ("\ xa6 \ xb8 \ xb1 \ x60 \ xa5 \ xce \ xb0 \ xea \ xa6 \ x72 \ xbc \ xd0 \ xb7 \ xc7 \ xa6 \ x72 \ xc5 \ xe9 \ xaa \ xed ") // {encoding: "Big5", confidence: 0.99}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.