靜態方法:Buffer.from(string[, encoding])
添加於:v5.10.0
參數
創建一個包含 string
的新 Buffer
。 encoding
參數標識將string
轉換為字節時要使用的字符編碼。
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from('this is a tést');
const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf1.toString());
// Prints: this is a tést
console.log(buf2.toString());
// Prints: this is a tést
console.log(buf1.toString('latin1'));
// Prints: this is a tést
const { Buffer } = require('node:buffer');
const buf1 = Buffer.from('this is a tést');
const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf1.toString());
// Prints: this is a tést
console.log(buf2.toString());
// Prints: this is a tést
console.log(buf1.toString('latin1'));
// Prints: this is a tést
如果 string
不是字符串或適合 Buffer.from()
變體的其他類型,則會拋出 TypeError
。
相關用法
- Node.js Buffer.from(array)用法及代碼示例
- Node.js Buffer.from()用法及代碼示例
- Node.js Buffer.from(object[, offsetOrEncoding[, length]])用法及代碼示例
- Node.js Buffer.from(arrayBuffer[, byteOffset[, length]])用法及代碼示例
- Node.js Buffer.from(buffer)用法及代碼示例
- Node.js Buffer.fill()用法及代碼示例
- Node.js Buffer.writeInt16BE()用法及代碼示例
- Node.js Buffer.writeDoubleBE()用法及代碼示例
- Node.js Buffer.entries()用法及代碼示例
- Node.js Buffer.writeUInt16LE()用法及代碼示例
- Node.js Buffer.allocUnsafe()用法及代碼示例
- Node.js Buffer.byteLength()用法及代碼示例
- Node.js Buffer.isBuffer()用法及代碼示例
- Node.js Buffer.writeUInt32BE()用法及代碼示例
- Node.js Buffer.equals()用法及代碼示例
- Node.js Buffer.values()用法及代碼示例
- Node.js Buffer.isEncoding()用法及代碼示例
- Node.js Buffer.isEncoding(encoding)用法及代碼示例
- Node.js Buffer.concat(list[, totalLength])用法及代碼示例
- Node.js Buffer.subarray()用法及代碼示例
- Node.js Buffer.writeDoubleLE()用法及代碼示例
- Node.js Buffer.includes()用法及代碼示例
- Node.js Buffer.readInt32BE()用法及代碼示例
- Node.js Buffer.writeIntLE()用法及代碼示例
- Node.js Buffer.swap16()用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 Buffer.from(string[, encoding])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。