天天看点

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());

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

继续阅读