alloc(size,fill,encoding)可以分配一个大小为 size 字节的新建的 Buffer,size默认为0
var buf = Buffer.alloc(10); 参数fill为填充的数据,只要指定了fill就会调用Buffer.fill(fill) 初始化这个Buffer对象
var buf = Buffer.alloc(10,0xff);//可以为十六进制的数据 allocUnsafe()方法
Unsafe(size)顾名思义就是不安全的方法,因为以这种方式创建的 Buffer 实例的底层内存是未初始化的。甚至可能包含到敏感数据,所以通过fill()方法帮助初始化
buf = Buffer.allocUnsafe(10);
buf.fill(0);
allocUnsafeSlow()方法allocUnsafeSlow()就是不从buffer缓冲区里分配,直接从操作系统分配,Slow指的是没有从缓冲池里高效分配
buf = Buffer.allocUnsafeSlow(10); from()方法
from()方法可以分配一个buffer对象,用来存放这个字符串的二进制对象,因此Buffer的内容可以通过[]进行访问
buf = Buffer.from("HelloWorld!");//from(array)
console.log(buf);
buf = Buffer.from([123,22,24,36]);
console.log(buf);
//重建一个buffer,把原来Buffer的数据拷贝给新的buffer
buf2 = Buffer.from(buf);
console.log(buf2);
//buf[index] index取值范围[0,len-1]
console.log(buf[0],buf[1]);
大尾与小尾形式写入存储buf.write(String,offset,length,encoding);
writeInt32BE(value,offset)第一个参数为写入的数据,第二个参数从哪个位置开始写入 ,表示其以大尾(大端)形式写入
writeInt32LE(value,offset)以小尾(小端)的形式写入数据
//以大尾的形式存放,4个字节的整数
buf.writeInt32BE(65535,0);
console.log(buf);
//以小尾的方式写入
buf.writeInt32LE(65535,0);
console.log(buf);
readInt32LE(offset)是指以小尾整型形式读取数据
readFloatLE(offset)是指以小尾浮点形式读取数据
var value = buf.readInt32LE(0);
console.log(value);
buf.writeFloatLE(3.16,0);
console.log(buf.readFloatLE(0));
//读取长度
var len = Buffer.byteLength("HelloWorld");
console.log(len);
buf = Buffer.alloc(4*4);
buf.writeInt32LE(65535,0);
buf.writeInt32LE(65535,4);
buf.writeInt32LE(65535,8);
buf.writeInt32LE(65535,12);
console.log(buf);
buf.swap32();
console.log(buf);
//用高位的方式读取
console.log(buf.readInt32BE(0));
console.log(buf.readInt32BE(4));
console.log(buf.readInt32BE(8));
console.log(buf.readInt32BE(12));
for (var i of buf.values()) {
console.log(i);
}
console.log(buf.toString('hex'));
console.log(buf.toJSON());
buf.fill('A');
console.log(buf);
console.log(buf.toString('utf8'));
判断是否为Buffer对象:
Buffer.isBuffer(obj);
获取字节长度:
Buffer.byteLength(string,encoding);
截取:
buf.slice(start,end);
拼接:
buf.concat(buf1,totalsize);//totalsize是指处理后buf大小总和;
相关知识
node.js毕设基于SpringBoot框架的宠物托运平台 论文+程序
基于Node.js的宠物生活社区系统
node.js毕设宠物之家综合平台程序+论文
Machine Learning(二):宠物智能识别之 Using OpenCV with Node.js
NPM安装模块报错:Error: sha1
node.js毕设宠物医院挂号系统程序+论文
Node.js宠物项目实战教程
node.js毕设宠物用品网上商城的设计与实现程序+论文
基于STM32的智能宠物自动喂食器设计思路:TCP\HTTP、Node.js技术
node.js毕设遇见宠物生活馆系统设计与实现(程序+论文)
网址: Node.js Buffer模块详解 https://m.mcbbbk.com/newsview801932.html
上一篇: MYSQL group by 报 |
下一篇: ERROR in ./src/l |