2.nodejs权威指南--Buffer

时间:2021-07-28 05:26:53

1. Buffer

1.1 创建

var buf1 = new Buffer(100);

var buf2 = new Buffer([0,1,2]);

var buf3 = new Buffer('hello world','utf8');

1.2 string相互转换

var buffer = new Buffer('node','utf8');

var str = buffer.toString('utf8');

1.3 json对象转换

var buffer = new Buffer('node');

var json = JSON.stringify(buffer);

var copy = new Buffer(JSON.parse(json));

copy.toString();

1.4 Buffer类方法

Buffer.isBuffer(obj); //检查是否buffer

Buffer.byteLength(string,[encoding]); //计算指定字符串的字节数

Buffer.concat(list,[totalLength]); //将几个Buffer对象结合成一个新的Buffer对象

Buffer.isEncoding(encoding); //检查是否为有效的编码格式