Buffer.from(str[, encoding])


Buffer.from(str[, encoding])
Node.js FS模块方法速查

  • str {String} 需要编码的字符串
  • encoding {String} 编码时用到,默认:'utf8'
创建一个新的 Buffer 包含给定的 JavaScript 字符串 str。如果提供 encoding 参数,将标识字符串的字符编码。如果没有提供 encoding 参数,默认为 'utf8'。 ``` const buf1 = Buffer.from('this is a tést'); console.log(buf1.toString()); // prints: this is a tést console.log(buf1.toString('ascii')); // prints: this is a tC)st

const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf2.toString());
// prints: this is a tést

如果 str 不是一个有效的 String 则抛出一个 TypeError 错误。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM