Node. js uses Buffer to encode and decode binary data.

Source: Internet
Author: User

Node. js uses Buffer to encode and decode binary data.

JavaScript is good at processing strings, but it is not very good at processing binary data because it was originally designed to process HTML documents. JavaScript has no byte type, no structured type (structured types), or even no byte array, only numbers and strings. (Original: JavaScript doesn't have a byte type-it just has numbers-or structured types, or http://skylitecellars.com/even byte arrays: It just has strings .)

Because Node is based on JavaScript, it can naturally process text protocols like HTTP, but you can also use it to interact with databases, process images or file uploads, etc. As you can imagine, how difficult it is to use strings to do these tasks. Earlier in the year, Node encoded byte into text characters to process binary data. However, this method was proved to be not feasible, which wastes resources, is slow, and not flexible, and is difficult to maintain.

Node has a binary Buffer to implement Buffer. This pseudo class provides a series of APIS for processing binary data, which simplifies the tasks for processing binary data. The buffer length is determined by the length of the byte data, and you can randomly set and obtain the byte data in the buffer.

Note: The Buffer class has a special place. The memory occupied by the byte data in the Buffer is not allocated in the memory of the JavaScrp.

On the It VM memory stack, that is to say, these objects will not be processed by the JavaScript garbage collection algorithm. Instead, they will be replaced by a permanent memory address that will not be modified, this also avoids CPU waste caused by memory replication of buffered content.

Create a buffer

You can create a buffer with a UTF-8 string like this:
Copy codeThe Code is as follows:
Var buf = new Buffer ('Hello World! ');

You can also use a specified encoded string to create a buffer:
Copy codeThe Code is as follows:
Var buf = new Buffer ('8b76fde713ce ', 'base64 ');

Acceptable character codes and identifiers are as follows:

1. ascii -- ASCI, applicable only to the ASCII character set.
2. utf8 -- UTF-8, which applies to any character set in the Unicode Character Set, has become the preferred encoding for the Web world and is also the default encoding type for Node.
3. base64 -- Base64. This encoding is based on 64 printable ASCII characters to represent binary data. Base64 is usually used to embed binary data that can be converted into strings in character documents, when necessary, it can be completely and lossless converted back to the original binary format.

If there is no data to initialize the buffer, you can use the specified capacity to create an empty Buffer:
Copy codeThe Code is as follows:
Var buf = new Buffer (1024); // create a 1024-byte Buffer.

Obtain and set buffered data

After creating or receiving a buffer object, you may want to view or modify its content. You can use the [] operator to access a buffer byte:
Copy codeThe Code is as follows:
Var buf = new Buffer ('My buffer content ');
// Access buffer contains 10th bytes
Console. log (buf [10]); //-> 99

Note: When you (using the buffer capacity) Create an initialized buffer, note that the buffered data is not initialized to 0, but random data.

Copy codeThe Code is as follows:
Var buf = new Buffer (1024 );

Console. log (buf [100]); //-> 5 (a random value)

You can modify the data at any location in the buffer as follows:

Copy codeThe Code is as follows:
Buf [99] = 125; // set the value of 100th bytes to 125

Note: In some cases, some buffer operations do not produce errors, such:

1. The maximum byte value in the buffer is 255. If a byte is given a number greater than 256, It is modeled by 256 and then the result is assigned to the byte.
2. If the value of a buffer byte is 256, the actual value will be 0)
3. If a floating point value is used to assign a value to a byte in the buffer, for example, 100.7, the actual value will be the integer part of the floating point value -- 100
4. If you try to assign a value to a location that exceeds the buffer capacity, the value assignment operation will fail and the buffer will not be modified.

You can use the length attribute to obtain the buffer length:
Copy codeThe Code is as follows:
Var buf = new Buffer (100 );

Console. log (buf. length); //-> 100

You can also use the buffer length to iterate the buffer content to read or set each byte:

Copy codeThe Code is as follows:
Var buf = new Buffer (100 );

For (var I = 0; I <buf. length; I ++ ){

Buf [I] = I;

}

The above Code creates a buffer containing 100 bytes and sets each byte in the buffer from 0 to 99.

Split and buffer data

Once a buffer is created or received, you may need to extract a portion of the buffered data. You can specify the starting position to split the existing buffer and create another small buffer:
Copy codeThe Code is as follows:
Var buffer = new Buffer ("this is the content of my buffer ");

Var smallerBuffer = buffer. slice (8, 19 );

Console. log (smallerBuffer. toString (); //-> "the content"

Note: When splitting a buffer, no new memory is allocated or copied. The new buffer uses the parent buffer memory, which is only used by the parent buffer to buffer certain data segments (specified by the starting position). This section contains several meanings.

First, if your program modifies the content of the parent buffer, these modifications will also affect the related sub-buffer, because the parent buffer and sub-buffer are different JavaScript objects, therefore, it is easy to ignore this issue and cause some potential bugs.

Second, when you use this method to create a small sub-buffer from the parent buffer, the parent buffer object will still be retained after the operation is completed and will not be reclaimed. If you do not pay attention to it, it is easy to cause memory leakage.

Note: If you are worried about memory leakage, you can use the copy method to replace slice operations. Next we will introduce copy.

Copy buffered data

You can use copy to copy a part of the buffer to another buffer as follows:
Copy codeThe Code is as follows:
Var buffer1 = new Buffer ("this is the content of my buffer ");

Var buffer2 = new Buffer (11 );

Var targetStart = 0;

Var sourceStart = 8;

Var sourceEnd = 19;

Buffer1.copy (buffer2, targetStart, sourceStart, sourceEnd );

Console. log (buffer2.toString (); //-> "the content"

The code above copies the 9th to 20 bytes of the source buffer to the starting position of the target buffer.

Decodes buffered data

Buffer data can be converted to a UTF-8 string like this:
Copy codeThe Code is as follows:
Var str = buf. toString ();

You can also specify the encoding type to decode the buffered data into any encoding type. For example, if you want to decode a buffer into a base64 string, you can do this:

Copy codeThe Code is as follows:
Var b64Str = buf. toString ("base64 ");

Using the toString function, you can also transcode A UTF-8 string to a base64 string:
Copy codeThe Code is as follows:
Var utf8String = 'my string ';

Var buf = new Buffer (utf8String );

Var base64String = buf. toString ('base64 ')

Summary

Sometimes, you have to deal with binary data, but native JavaScript does not have a clear way to do this, so Node provides a Buffer class that encapsulates some operations for contiguous memory blocks. You can split or copy memory data between two buffers.

You can also convert a buffer into a encoded string or, in turn, convert a string into a buffer to access or process each bit.


The decoding of genetic algorithms is only for binary encoding or all genetic algorithms must be decoded.

It should be said that all genetic algorithms must be encoded and decoded. Genetic Algorithms simulate biological genetic and evolutionary processes, and crossover is the core operation of genetic algorithms. A crossover process is an operation between binary data, such as; And, Or, different or ...... Encoding aims to realize the ing from phenotype to genotype, so as to facilitate genetic operation. Then, select the next generation through the fitness function and iterate in sequence until the appropriate child generation is reached. In this case, the child is still binary. Therefore, the child must be mapped to the phenotype through decoding.

What is the use of nodejs buffer?

Buffer is used as a buffer to process non-UTF-8 characters or binary data.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.