Home  >  Article  >  Web Front-end  >  A brief discussion on the usage of the Buffer module in Nodejs and see how to encode and decode binary data packets

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

青灯夜游
青灯夜游forward
2021-11-09 09:39:532414browse

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));

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]));

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);

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 position is big endian

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

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 position is little endian

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

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!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete