概念:
Bufer 是一个类似于数组的 对象,用于表示固定长度的字节序列
Buffer 本质是一段内存空间,专门用来处理 二进制数据。
特点:
1,Buffer 大小固定且无法调整
2,Bufer 性能较好,可以直接对计算机内存进行操作
3,每个元素的大小为 1字节 (byte)
//1.alloc
let buf = Buffer.alloc(10);
console.log(buf);
//<Buffer 00 00 00 00 00 00 00 00 00 00>
//2.allocUnsafe 含有旧的数据
let buf_2 = Buffer.allocUnsafe(10);
console.log(buf_2);
//<Buffer 00 00 00 00 00 00 00 00 00 00>
//3.from
let buf_3 = Buffer.from("hello");
console.log(buf_3);
//<Buffer 68 65 6c 6c 6f>
//4.Buffer与字符串的转换
let buf_4 = Buffer.from([105, 108, 111, 118, 101, 121, 111, 117]);
console.log(buf_4.toString()); //utf-8
//iloveyou
//5.[]
let buf_5 = Buffer.from("hello");
console.log(buf_5[0]); //打印Buffer中h的数据 最后以十进制显示 104
console.log(buf_5[0].toString(2)); //转成二进制 1101000(实际上是01101000)
console.log(buf_5); //<Buffer 68 65 6c 6c 6f>
buf_5[0] = 98;
console.log(buf_5); //<Buffer 62 65 6c 6c 6f>
console.log(buf_5.toString()); //bello