node. JS's own ToString () method does not support GBK, so it is necessary to load a third-party library when converting to Chinese, the following two encoding libraries, iconv-lite and encoding are recommended.
What are iconv, iconv-lite and encoding? iconv: is a standard character set conversion interface in Unix-like systems for converting between different character set encodings, first appearing in the HP-UX system. iconv-lite: is a pure JS implementation of ICONV, the supported encodings include node. JS native code: UTF8, UCS2, ASCII, binary, base64; support for widely used 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 。 Officials claim to be quicker than node-iconv. encoding: Is the Node-iconv and iconv-lite again encapsulation, encoding first call Node-iconv, if Node-iconv cannot parse, then call Iconv-lite as an alternative. iconv-lite installation uses 1. Installs the $ npm install iconv-lite 2. Use var Iconv = require (' Iconv-lite '); //Convert from a encoded buffer to JS STRING.STR = Iconv.decode (buf, ' WI n1251 '); //Convert from JS string to an encoded BUFFER.BUF = Iconv.encode ("Sample input string", ' win1251 '); // Check if encoding is supportediconv.encodingexists ("Us-ascii") to copy code 3. Performance comparison of Iconv-lite and Node-iconv operation [EMAIL&NBSp;protected] [email protected] ---------------------------------------------------------- Encode (' win1251 ') ~115 MB/s ~230 mb/sdecode (' win1251 ') ~95 MB/s &N bsp;~130 mb/s encoding installation uses 1. Installs the $ npm install encoding 2. Use the encoding module to convert () in one way, using the following method: Encoding.convert (text, Tocharset, Fromcharset). text: The object that needs to be converted, either a buffer or a string object. Tocharset: the converted encoding. Fromcharset: The encoding before conversion, the default 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> Chapter:jschardet what is Jschardet? jschardet is the jschardet of António Afonso from Python Chardet to JavaScript. 1. Installs the $ npm install jschardet 2. Use var Jschardet = require ("Jschardet") //"Àíàçã" in Utf-8jschardet.detect ("\xc3\xa0\xc3\xad\xc3\ Xa0\xc3\xa7\xc3\xa3 ")//{Encoding: "Utf-8", confidence:0.9690625} //"secondary common standard" 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}
Node. JS Encoding Conversion