In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.