node——buffer

buffe facilitate data transmission, data can be transmitted as part of a disposable
one. Type Description
1.javascript no language or reading binary data manipulation mechanisms.
2.Node.js introduced Buffer type allows us to operate TCP stream file or stream
objects 3.Buffer type similar group of integers, but the size of the Buffer is fixed, and allocate physical memory heap V8 outside. BUffer size is determined when they are created, and can not be adjusted (buf.length is fixed and can not be modified)
4.Buffer is global, so use the time without having to require way to load
Second, create BUffer objects
1. by Buffer.from () to create a Buffer objects
Buffer.from (string [, encoding])

var ARRY = [0x68,0x6c, 0x6C, 0x6F, 0x20 ];
 var buf = Buffer.from (ARRY); // Switch Buffer type 
console.log (buf);
console.log(buf.toString('utf8'));

var buff=Buffer.from('你好!');
console.log(buff);
console.log(buff.toString('utf8'));

2. Buffer stitching multiple objects into one object

var ARRY = [0x68,0x6c, 0x6C, 0x6F, 0x20 ];
 var buf = Buffer.from (ARRY); // Switch Buffer Type

var BUFF = Buffer.from ( 'Hello!' );

var bufferlist=[buf,buff];
var buffe=Buffer.concat(bufferlist);
console.log(buffe.toString('utf8'));

3. Get string corresponding to the number of bytes

A Chinese 3 bytes, symbols or numbers, or a 1 byte

var len=Buffer.byteLength('hello世界!');
console.log (s);

only 12 =

4. Determination of a target object type Buffer

Buffer.isBuffer(obj)

Return is true, compared with Buffer, not vice versa

5. Obtain the corresponding byte Buffer (buf object name)

buf[index]

Array-like

6. Get the number of bytes Buffer object (buf object name)

buf.length

buf.length property can not be modified

7.Buffer object coding

Node.js is currently supported coded as follows:

1.ascil

2.utf8

3.utf16le

 . ucs2 alias is utf16le

4.base64

5.latin1

 . binary is an alias for Latin1

6.hex

 . It is represented by two hexadecimal bytes each

var buf1 = Buffer.from ( 'Hello World, the Hello World!', 'utf8' );
console.log(buf1.toString('hex'));
console.log(buf1.toString('base64'));
console.log(buf1.toString('utf8'));

 

 

Guess you like

Origin www.cnblogs.com/ellen-mylife/p/11013275.html