📜  Node.js Buffer.byteOffset 属性

📅  最后修改于: 2022-05-13 01:56:51.795000             🧑  作者: Mango

Node.js Buffer.byteOffset 属性

Buffer.byteOffset属性是缓冲区模块中Buffer类的内置应用程序编程接口,用于获取此缓冲区的字节偏移值。

句法:

const Buffer.byteOffset

返回值:该属性返回数组缓冲区的对象。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// Buffer.byteOffset property
  
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
  
// Getting buffer object form existing 
// arraybuffer object
const buffer = Buffer.from(arrbuff);
  
// Getting byteoffset of buffer
// by using byoffset property
const bytoff = buffer.byteOffset;
  
// Display the result
console.log("byteoffset is : " + bytoff);


Javascript
// Node.js program to demonstrate the
// Buffer.byteOffset property
  
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
  
// Getting buffer object form existing 
// arraybuffer object
const buffer = Buffer.from(arrbuff);
  
// Getting byteoffset of buffer
// by using byoffset api
const bytoff = buffer.byteOffset;
  
const buff = new Int8Array(buffer, bytoff, buffer.length);
  
// Display the result
console.log("Int8Arry object : " + buff);


输出:

byteoffset is : 0

示例 2:文件名:index.js

Javascript

// Node.js program to demonstrate the
// Buffer.byteOffset property
  
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
  
// Getting buffer object form existing 
// arraybuffer object
const buffer = Buffer.from(arrbuff);
  
// Getting byteoffset of buffer
// by using byoffset api
const bytoff = buffer.byteOffset;
  
const buff = new Int8Array(buffer, bytoff, buffer.length);
  
// Display the result
console.log("Int8Arry object : " + buff);

输出:

Int8Arry object : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

使用以下命令运行index.js文件:

node index.js

参考: https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buf_byteoffset