• string {string} 要编码的字符串。
    • encoding {string} string 的字符编码。默认值: 'utf8'

    创建一个包含 string 的新 Bufferencoding 参数指定用于将 string 转换为字节的字符编码。

    1. const buf1 = Buffer.from('this is a tést');
    2. const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
    3. console.log(buf1.toString());
    4. // 打印: this is a tést
    5. console.log(buf2.toString());
    6. // 打印: this is a tést
    7. console.log(buf1.toString('latin1'));
    8. // 打印: this is a tést

    如果 string 不是一个字符串或适用于 Buffer.from() 变量的其他类型,则抛出 TypeError