How to Use the buffer. copy method in node. js _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces node. buffer in js. the usage of the copy method. This article introduces the buffer. copy method description, syntax, receive parameters, use instances, and implement Source Code. For more information, see Method description:

Replace the buffer.

Copy data from the source buffer and replace it with the specified location of the target buffer.

Syntax:

The Code is as follows:


Buffer. copy (targetBuffer, [targetStart], [sourceStart], [sourceEnd])

Receiving parameters:

TargetBuffer: The target buffer, which is replaced by the buffer for replication.

The starting position of the target buffer data replacement by targetStart

Source buffer Data Replication start position

End position of sourceEnd source buffer Data Replication

Example:

In this example, the data between buf1 16 and 20 is extracted, the data is copied to buf2, and is replaced from position 8 of buf2.

The Code is as follows:


Buf1 = new Buffer (26 );
Buf2 = new Buffer (26 );
For (var I = 0; I <26; I ++ ){
Buf1 [I] = I + 97; // 97 is ASCII
Buf2 [I] = 33; // ASCII!
}
Buf1.copy (buf2, 8, 16, 20 );
Console. log (buf2.toString ('ascii ', 0, 25 ));

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.