Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of the Buffer module in Nodejs

Shulou Source: shulou.com Published: 2022-06-02 16:50:32 10月05日 Update

This article mainly introduces "what is the usage of Buffer module in Nodejs". In daily operation, I believe many people have doubts about the usage of Buffer module in Nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the usage of Buffer module in Nodejs?" Next, please follow the editor to study!

Today, we will learn about the use of Nodejs's Buffer module, as well as the more commonly used buffer npm package.

First, attach the document address of nodejs buffer: https://nodejs.org/dist/latest-v16.x/docs/api/buffer.html

How to create a buffer

There are two common ways to create a buffer:

1. Buffer.alloc (size): create a buffer of specified length

Console.log (Buffer.alloc (20))

Results:

2. Buffer.from (): create a buffer from an existing structure

Console.log (Buffer.from ("fsfdfafd")); console.log (Buffer.from ([1,2,3,4]))

Results:

Buffer read and write buf.writeInt8 (value, offset)

Value: the number to write to the buffer

Offset: the number of bytes to skip before starting writing

Let buffer1 = Buffer.from ("fsfdfafd"); console.log (buffer1); buffer1.writeInt8 (127,1); console.log (buffer1)

Results:

From the result, we can see that the content of the location where offset is 1 has been changed.

Buf.writeInt16BE (value, offset)

Value: the number to write to the buffer

Offset: the number of bytes to skip before starting writing

Large end at the specified offset location

Let buffer1 = Buffer.from ("fsfdfafd"); console.log (buffer1); buffer1.`writeInt16BE` (127,1); console.log (buffer1)

Results:

Buf.writeInt16LE (value, offset)

Value: the number to write to the buffer

Offset: the number of bytes to skip before starting writing

Small end at the specified offset location

Let buffer1 = Buffer.from ("fsfdfafd"); console.log (buffer1); buffer1.`writeInt16LE` (127,1); console.log (buffer1)

Results:

Buffer npm package

Above we briefly introduced the creation and reading and writing methods of buffer, but in our daily development process, it is too troublesome to use the method described above to encode binary files, so here we introduce the npm package for writing buffer.

The NPM package about node buffer that we are going to introduce here is protocol-buffers. This is its npm address: https://www.npmjs.com/package/protocol-buffers

At this point, the study on "what is the use of the Buffer module in Nodejs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Results modules methods learning locations bytes numbers addresses commonly used more help practical next binary content big end files documents articles are in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Microsoft macOS MySQL