NODEJS Encoding Conversion problem

Source: Internet
Author: User
Tags base64

Currently, node. JS supports only hex, UTF8, ASCII, binary, base64, UCS2 encoding conversions . For gbk,gb2312 encoding, Nodejs's own ToString () method is not supported, so the Chinese conversion needs to load a third-party library, mainly has two encoding conversion library iconv-lite and encoding, personal recommended encoding.

Iconv-lite: is a pure JS implementation of the ICONV, the supported encodings include node. JS native code: UTF8, UCS2, ASCII, Binary, Base64; supports 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.

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.

Encoding installation and use

1. Installation

NPM Install encoding

2. Use

The encoding module is a method of convert (), 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 result is a Buffer object.

1 var encoding = require (' encoding '); 2 3 var result = Encoding.convert ("Õäöü", "latin_1"); 4 // <buffer d5 c4 d6 dc>

Here is an example of how encoding is used:

1.utf8 Turn GBK

For example, "Su A00001" the corresponding form of the GBK is "CBD5413030303031".

1 varFS = require (' FS ');2 varEncodingconvert = require (' encoding '));3 varBUF = require (' buffer ');4 vartemp = "Su A00001";5 varresult =NewString ();6 7 varResultbuffer = Encodingconvert.convert (temp, "GBK", "UTF8");8Console.log ("The Resultbuffer is:", Resultbuffer);9 Ten  for(vari = 0;i < resultbuffer.length;i++) One { A     -Result+=resultbuffer.slice (i,i+1) [0].tostring (16); -   the } -result =result.touppercase (); -  -Console.log ("The result is:", result);

That is, the 8th line of Resultbuffer is a buffer, the content is the corresponding "CBD5413030303031", but only to the 16th line to capitalize conversion.

The function of line 13th is to take each of the Buffer, the result of Resultbuffer.slice (i) is <buffer cb>,<buffer D5>,<buffer 41>,<buffer 30 >,<buffer 30>,<buffer 30>,<buffer 30>,<buffer 31>.

Resultbuffer.slice (i) [0] The result is 203,213,65,48,48,48,48,49, because buffer is binary, this result is the binary corresponding decimal result, and then convert it into 16 binary is the final result.

2.GBK Turn UTF8

1 varFS = require (' FS ');2 varEncodingconvert = require (' encoding '));3 varBUF = require (' buffer ');4 5 vartemp = "CBD5413030303031";6 varBufferarray = [0xcb,0xd5,0x41,0x30,0x30,0x30,0x30,0x31];7 varBuffer =NewBuffer (bufferarray);8Console.log ("The buffer is:", buffer);9 Console.log (buffer.tostring ());Ten  One varplatedecoded = Encodingconvert.convert (buffer, "UTF8", "GBK"); A              -Console.log ("The result is:", platedecoded.tostring ());

NODEJS Encoding Conversion problem

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.