Buffer.byteLength()方法用于返回指定缓冲区对象的字节长度。
用法:
Buffer.byteLength( string, encoding )
参数:该方法接受上述和以下所述的两个参数:
- String它是必需的参数,用于指定对象以计算缓冲区的长度。支持的字符串类型为String,Buffer,TypedArray,DataView和ArrayBuffer。
 - Encoding:它是可选参数。如果对象是字符串,则此参数指定其编码方案。编码方案的默认值为“utf8”。
 
返回值:它返回指定对象的字节数。
范例1:
// Node.js program to demonstrate the 
// Buffer.bytelength() method 
  
// Create a buffer 
var buf = Buffer.alloc(20); 
  
// Check the length of a buffer object:
var lenobj = Buffer.byteLength(buf); 
  
console.log(lenobj);输出:
20
范例2:
// Node.js program to demonstrate the 
// Buffer.bytelength() method 
  
// Check the length of a buffer object:
var len = Buffer.byteLength('GeeksForGeeks'); 
  
console.log(len);输出:
13
注意:
- 在Node.js v7.0.0中,无效的输入参数将引发错误。
 - 在Node.js v5.10中,字符串参数的值可以是任何TypedArray,DataView或ArrayBuffer。
 - 此方法已添加到node.js v0.1.90。
 
相关用法
注:本文由纯净天空筛选整理自priyanshid1大神的英文原创作品 Node.js | Buffer.byteLength() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
