Buffer (Buffer)
- Buffer array structure and the like, and a method of operation is also similar to an array
- the array can not be stored in a binary file, and the Buffer is designed to store binary data
- using the buffer does not require the introduction of a template, i.e., directly may be
- are binary data stored in the buffer, but are displayed on the display 16 in the form of hexadecimal
range of each element of the buffer is from 00 - FF 0 - 255
00000000 - 11111111
computer a 0 or a 1 we called a bit (bit)
8bit = 1byte (bytes)
1024byte 1KB =
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
element in the buffer, one byte of memory
- Buffer size, once determined, can not be modified, Buffer is actually the direct operation of the underlying memory
var STR = "Su Zai the Hello" ; // a string stored into buffer var buf = Buffer.from (STR); the console.log (to buf.length); // The memory space console.log (str. length); // string length console.log (buf);
// Create a specified size Buffer // Buffer construct functions are not recommended var BUF2 = new new Buffer (10); // 10-byte Buffer the console.log (buf2.length);
//创建一个10个字节的buffer var buf2 = Buffer.alloc(10); //通过索引,来操作buf中的元素 buf2[0] = 88; buf2[1] = 255; buf2[2] = 0xaa; buf2[3] = 255; //只要数字在控制台或页面输出一定是10进制,toString可以转换进制 console.log(buf2[2].toString(16));
for(var i =0;i<buf2.length;i++){ console.log(buf2[i]); }
//Buffer.allocUnsafe(size)创建一个指定大小的buffer,但是buffer中可能含有敏感数据 var buf3 = Buffer.allocUnsafe(10); console.log(buf3);
Buffer.from(str) 将一个字符串转换为buffer
Buffer.alloc(size) 创建一个指定大小的Buffer
Buffer.alloUnsafe(size) 创建一个指定大小的Buffer,但可能包含敏感数据
buf.toString() 将缓冲区中的数据转换为字符串
var buf4 = Buffer.from("我是一段文本数据"); console.log(buf4.toString());
注:具体操作用到Buffer缓冲区时,参考:http://nodejs.cn/api/buffer.html 官方文档查询使用方法