Buffer.from(str[, encoding])
- str {String} 需要编码的字符串
- encoding {String} 编码时用到,默认:'utf8'
const buf1 = Buffer.from('this is a tést');console.log(buf1.toString());// prints: this is a téstconsole.log(buf1.toString('ascii'));// prints: this is a tC)stconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');console.log(buf2.toString());// prints: this is a tést
如果 str 不是一个有效的 String 则抛出一个 TypeError 错误。