js Buffer常用方法

//构造
//alloc构造              10个空间 16进制格式,所以数字16显示为10
var buf1=Buffer.alloc(10,16);
console.log(buf1);

//allocUnsafe构造
//分配10个空间,但空间内的数据不一定为空
var buf2=Buffer.allocUnsafe(10);
//类似于java里面StringBuffer的append
buf2.write("123");
console.log(buf2);

//from构造(常用)
var buf3=Buffer.from([1,2,3,4,5]);
console.log(buf3);


//Buffer清空方法
//buf1.fill();
//
//Buffer转JSON方法
var bufJ=Buffer.from([1,2,3,4,5]);
bufJ.toJSON();
let xx=JSON.stringify(bufJ);
console.log(xx);

//concat
let bufc1=Buffer.from("中文");
let bufc2=Buffer.from("拼接");
let bufc3=Buffer.concat([bufc1,bufc2]);
console.log(bufc3);

其他详细方法

猜你喜欢

转载自blog.csdn.net/chijiajing/article/details/83341505