buf.toString([encoding[, start[, end]]])
添加於:v0.1.90
參數
encoding<string> 要使用的字符編碼。 默認:'utf8'。start<integer> 開始解碼的字節偏移量。 默認:0。end<integer> 停止解碼的字節偏移量(不包括在內)。 默認:。buf.length- 返回: <string>
根據 encoding 中指定的字符編碼將 buf 解碼為字符串。 start 和 end 可以被傳遞以僅解碼 buf 的子集。
如果 encoding 是 'utf8' 並且輸入中的字節序列不是有效的 UTF-8,則每個無效字節都將替換為替換字符 U+FFFD 。
字符串實例的最大長度(以 UTF-16 代碼單元表示)可用作 。buffer.constants.MAX_STRING_LENGTH
import { Buffer } from 'node:buffer'; const buf1 = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } console.log(buf1.toString('utf8')); // Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('utf8', 0, 5)); // Prints: abcde const buf2 = Buffer.from('tést'); console.log(buf2.toString('hex')); // Prints: 74c3a97374 console.log(buf2.toString('utf8', 0, 3)); // Prints: té console.log(buf2.toString(undefined, 0, 3)); // Prints: téconst { Buffer } = require('node:buffer'); const buf1 = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } console.log(buf1.toString('utf8')); // Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('utf8', 0, 5)); // Prints: abcde const buf2 = Buffer.from('tést'); console.log(buf2.toString('hex')); // Prints: 74c3a97374 console.log(buf2.toString('utf8', 0, 3)); // Prints: té console.log(buf2.toString(undefined, 0, 3)); // Prints: té
相關用法
- Node.js Buffer buf.toJSON()用法及代碼示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
- Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代碼示例
- Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代碼示例
- Node.js Buffer buf.keys()用法及代碼示例
- Node.js Buffer buf.writeFloatLE(value[, offset])用法及代碼示例
- Node.js Buffer buf.indexOf(value[, byteOffset][, encoding])用法及代碼示例
- Node.js Buffer buf.readFloatLE([offset])用法及代碼示例
- Node.js Buffer buf.readInt32LE([offset])用法及代碼示例
- Node.js Buffer buf.writeInt8(value[, offset])用法及代碼示例
- Node.js Buffer buf.swap32()用法及代碼示例
- Node.js Buffer buf.writeInt32LE(value[, offset])用法及代碼示例
- Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代碼示例
- Node.js Buffer buf.values()用法及代碼示例
- Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代碼示例
- Node.js Buffer buf.length用法及代碼示例
- Node.js Buffer buf.byteOffset用法及代碼示例
- Node.js Buffer buf.readUInt32BE([offset])用法及代碼示例
- Node.js Buffer buf.readDoubleLE([offset])用法及代碼示例
- Node.js Buffer buf.writeFloatBE(value[, offset])用法及代碼示例
- Node.js Buffer buf.readBigUInt64LE([offset])用法及代碼示例
- Node.js Buffer buf.readUInt16BE([offset])用法及代碼示例
- Node.js Buffer buf.writeInt16BE(value[, offset])用法及代碼示例
- Node.js Buffer buf.swap64()用法及代碼示例
- Node.js Buffer buf.readUInt8([offset])用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 buf.toString([encoding[, start[, end]]])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
