Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Buffer in Node.js

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to parse Buffer in Node.js. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The JavaScript language itself has only string data types, not binary data types.

However, binary data must be used when dealing with streams such as TCP or files. So in Node.js, you define a Buffer class that is used to create a cache dedicated to binary data.

In Node.js, the Buffer class is a core library released with the Node kernel. The Buffer library provides a way for Node.js to store raw data, allowing Node.js to work with binary data, making it possible to use the Buffer library whenever you need to process data moved during an Ibank O operation in Node.js.

The raw data is stored in an instance of the Buffer class.

A Buffer is similar to an array of integers, but it corresponds to a piece of raw memory outside the V8 heap memory.

Create a Buffer class

The Node Buffer class can be created in several ways.

Method 1

Create a Buffer instance with a length of 10 bytes:

Var buf = new Buffer (10)

Method 2

Create a Buffer instance from the given array:

Var buf = new Buffer ([10, 20, 30, 40, 50])

Method 3

Create an Buffer instance with a string:

Var buf = new Buffer ("bianchengsanmei", "utf-8")

Utf-8 is the default encoding, and it also supports the following encodings: "ascii", "utf8", "utf16le", "ucs2", "base64" and "hex".

Write buffer

Grammar

The syntax for writing to the Node buffer is as follows:

Buf.write (string [, offset [, length]] [, encoding])

Parameters.

The parameters are described as follows:

String-the string written to the buffer.

Offset-the index value that the buffer starts writing, defaulting to 0.

Length-number of bytes written, default is buffer.length

Encoding-the encoding used. The default is' utf8'.

Return value

Returns the actual write size. If there is not enough buffer space, only part of the string will be written.

Example

Buf = new Buffer; len = buf.write ("bi"); len = buf.write ("bianchengsanmei"); console.log ("bytes written:" + len)

Execute the above code, and the output is as follows:

Number of bytes written by node main.js: 15 copy code reads data from buffer

Grammar

The syntax for reading Node buffer data is as follows:

Buf.toString ([encoding [, start [, end])

Parameters.

The parameters are described as follows:

Encoding-the encoding used. The default is' utf8'.

Start-specifies the location of the index to start reading, which defaults to 0.

End-end position, which defaults to the end of the buffer.

Return value

Decodes the buffer data and returns a string using the specified encoding.

Example

Buf = new Buffer (26); for (var I = 0; I < 26; iTunes +) {buf [I] = I + 97;} console.log (buf.toString ('ascii')); / / output: abcdefghijklmnopqrstuvwxyzconsole.log (buf.toString (' ascii',0,5)); / / output: abcdeconsole.log (buf.toString ('utf8',0,5)); / / output: abcdeconsole.log (buf.toString (undefined,0,5)) / / use 'utf8' encoding, and output: abcde

Execute the above code, and the output is as follows:

$node main.jsabcdefghijklmnopqrstuvwxyzabcdeabcdeabcde converts Buffer to JSON object

Grammar

The function syntax format for converting Node Buffer to JSON objects is as follows:

Buf.toJSON ()

Return value

Returns the JSON object.

Example

Var buf = new Buffer ('bianchengsanmei'); var json = buf.toJSON (buf); console.log (json)

Execute the above code, and the output is as follows:

{type: 'Buffer', data: [98, 105, 97, 110, 99, 104, 101, 110, 103, 115, 97, 110, 109, 101,105]} buffer merge

Grammar

The syntax for Node buffer merging is as follows:

Buffer.concat (list [, totalLength])

Parameters.

The parameters are described as follows:

List-list of arrays of Buffer objects used for merging.

TotalLength-specifies the total length of the merged Buffer object.

Return value

Returns a new Buffer object with multiple members merged.

Example

Var buffer1 = new Buffer ('programming Samadhi'); var buffer2 = new Buffer ('bi'); var buffer2 = new Buffer (' bianchengsanmei'); var buffer3 = Buffer.concat ([buffer1,buffer2]); console.log ("buffer3 content:" + buffer3.toString ())

Execute the above code, and the output is as follows:

Buffer3 content: programming Samadhi bianchengsanmei buffer comparison

Grammar

The function syntax for Node Buffer comparison is as follows, which was introduced in Node.js v0.12.2:

Buf.compare (otherBuffer)

Parameters.

The parameters are described as follows:

OtherBuffer-another Buffer object compared to the buf object.

Return value

Returns a number indicating that buf is before, after, or the same as otherBuffer.

Example

Var buffer1 = new Buffer ('ABC'); var buffer2 = new Buffer (' ABCD'); var result = buffer1.compare (buffer2); if (result < 0) {console.log (buffer1 + "before + buffer2 +");} else if (result = = 0) {console.log (buffer1 + "is the same as" + buffer2 + ");} else {console.log (buffer1 +" after + buffer2 + ");}

Execute the above code, and the output is as follows:

ABC copies buffers before ABCD

Grammar

The Node buffer copy syntax is as follows:

Buf.copy (target [, targetStart [, sourceStart [, sourceEnd]])

Parameters.

The parameters are described as follows:

TargetBuffer-the Buffer object to copy.

TargetStart-numeric, optional, default: 0

SourceStart-numeric, optional, default: 0

SourceEnd-numeric, optional, default: buffer.length

Return value

There is no return value.

Example

Var buffer1 = new Buffer ('ABC'); / / copy a buffer var buffer2 = new Buffer (3); buffer1.copy (buffer2); console.log ("buffer2 content:" + buffer2.toString ())

Execute the above code, and the output is as follows:

Buffer2 content: ABC buffer clipping

The Node buffer clipping syntax is as follows:

Buf.slice ([start [, end]])

Parameters.

The parameters are described as follows:

Start-numeric, optional, default: 0

End-numeric, optional, default: buffer.length

Return value

Returns a new buffer that points to the same block of memory as the old buffer, but is cut from index start to end.

Example

Var buffer1 = new Buffer ('youj'); / / clipping buffer var buffer2 = buffer1.slice (0Magne2); console.log ("buffer2 content:" + buffer2.toString ())

Execute the above code, and the output is as follows:

Buffer2 content: yo buffer length

Syntax Node buffer length calculation syntax is as follows:

Buf.length

Return value

Returns the length of memory occupied by the Buffer object.

Example

Var buffer = new Buffer ('bianchengsanmei'); / / buffer length console.log ("buffer length:" + buffer.length)

Execute the above code, and the output is as follows:

Buffer length: 15 on how to parse the Buffer in Node.js to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report