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