A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

青灯夜游
Release: 2021-11-09 09:39:53
forward
2346 people have browsed it

This article will take you through the usage of the Buffer module in Nodejs, and introduce the method of Buffer encoding and decoding binary data packets. I hope it will be helpful to everyone!

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

Today, we will learn about the use of Nodejs's Buffer module, as well as the currently commonly used buffer npm package. [Recommended study: "nodejs Tutorial"]

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 commonly used methods to create a buffer:

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

console.log(Buffer.alloc(20));
Copy after login

Result:

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

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

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

Result:

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

Buffer reading and writing

buf.writeInt8(value, offset)

value: The number to be written to the buffer


offset: The number of bytes to skip before starting to write

let buffer1 = Buffer.from("fsfdfafd"); console.log(buffer1); buffer1.writeInt8(127, 1); console.log(buffer1);
Copy after login

Result:

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

From the results, we can see that the content at offset 1 has been changed.

buf.writeInt16BE(value, offset)

value: the number to be written into the buffer


offset: the number to be written before starting to write The number of bytes skipped


at the specified

offset positionisbig endian

let buffer1 = Buffer.from("fsfdfafd"); console.log(buffer1); buffer1.`writeInt16BE`(127, 1); console.log(buffer1);
Copy after login

Result:

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

buf.writeInt16LE(value, offset)

value: The number to be written into the buffer


offset: Start writing The number of bytes to be skipped before


at the specified

offset positionislittle endian

let buffer1 = Buffer.from("fsfdfafd"); console.log(buffer1); buffer1.`writeInt16LE`(127, 1); console.log(buffer1);
Copy after login
Result:

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

buffer npm package

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

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

Since the documentation on npm has clearly introduced how to use the package, we will do it here. I won’t repeat it again. If you are interested, you can read it.

Conclusion

Today, we learned the simple methods of reading, writing and creating buffers, and also learned about the usage of node’s buffer package. Well, see you next time. study hard, improve every day!

A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!