node. JS API Learning Note (ii)

Source: Internet
Author: User

This article is published in my blog.

The previous note says to create a buffer instance, this section continues with buffer. This section explains some of the static methods, writes, and read methods for buffer.

Buffer.isencoding (encoding) to determine whether Nodejs supports this encoding

    Console.log (buffer.isencoding ("gb2312"));//No matter how node does not support this encoding, Rote    Console.log (buffer.isencoding ("UTF8"));  True    Console.log (buffer,isencoding ("ASCII"));//true

  

Buffer.isbuffer (obj) to determine if the Obj object is a buffer type Object

    var str = "";    var obj = new Buffer (+);    Console.log (Buffer.isbuffer (str));    False    Console.log (Buffer.isbuffer (obj));    

Buffer.bytelength (string,[encoding]) gets the specified byte for the specified encoding (default UTF8). Note that UTF8 differs from UCS2

    var str = "Solitary seeking defeat";    var buf = new Buffer (str);    Console.log (Buffer.bytelength (str, "UTF8"));    Console.log (Buffer.bytelength (str));    Console.log (Buffer.bytelength ("abc"));         3     Console.log (buffer.bytelength (str, "UCS2"));    

  

Buffer Write Method: WriteXXX (Value,offset,[noassert])

Value: must be plastic or floating point;

Offset: The start position of the write;

Noassert: Out of range packet exception, can be omitted;

There are the following methods: WriteInt8 (), Writeint16be (), Writeintle (), Writeint32le (), Writeint32be (). Note WriteInt8 () only this and the default is Le mode

    var buf = new Buffer (4);    var data = 0x0c;//Hex =12    buf.fill ();    Fill by using the Fill method, almost all clear 0 State    buf.writeint8 (data,0);    Console.log (BUF);    0c xx        Buf.fill ();    Buf.writeint16le (data,0);//Use 16-bit signed low-to-high storage    console.log (BUF);        0c xx        Buf.fill ();    Buf.writeint16be (data,0);//Use 16-bit signed high to low storage    console.log (BUF);        0c 00 00

  

Buffer Read Method ReadUInt8 (Offset,[noassert]) This corresponds to write one by one!

    var buf = new Buffer (4);    var data = 0x0c;    Buf.fill ();    Buf.writeint8 (data,0);    Console.log (buf.readint8 (0));         Buf.fill ();    Buf.writeint16le (data,0);    Console.log (Buf.readint16le (0)); 12

Copy (Targetbuffer,[targetstart],[sourcestart],[sourceend]) copies the specified position from a buffer object to the target buffer

    var sbuffer = new Buffer ("Lone Orphan");    var tBuffer1 = new Buffer (6);    var tBuffer2 = new Buffer (9);    Sbuffer.copy (tBuffer1);    Sbuffer.copy (tBuffer2);    Console.log (Tbuffer1.tostring ());    Solitary    Console.log (tbuffer2.tostring ());    Lone Lonely Beg

  

You can see that there is no above, with a comparison above, we know that the default UTF output is 3 bytes a Chinese Oh! So the number of bytes printed Sbuffer is 12. You can try to use the encoded ASCII output is how much, under what conditions is not output!

This class first come here, feel recently no time, slowly, persist.

node. JS API Learning Note (ii)

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.