- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢 (Where)
- Node.js - MySQL 排序 (Order By)
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢 (Join)
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制 (Limit)
- Node.js - MongoDB 連線查詢 (Join)
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - Buffer.toString() 方法
NodeJS 的Buffer.toString() 方法用於根據指定的編碼解碼字串。預設編碼為 'utf-8'。
語法
以下是Node.JS Buffer.toString() 方法的語法:
buf.toString([encoding[,start[,end]]])
引數
此方法接受三個引數,但都是可選的。具體解釋如下。
encoding − (可選) 要使用的編碼。預設編碼為 utf-8。
start − (可選) 解碼開始的起始索引。預設值為 0。
end − (可選) 解碼結束的結束索引。預設值為 buffer.length。
返回值
buffer.toString() 方法使用指定的編碼解碼緩衝區並返回字串。
示例
要建立緩衝區,我們將使用 NodeJS Buffer.from() 方法:
const buffer = Buffer.from('Hello');
console.log(buffer.toString('hex'));
輸出
使用的編碼是 'hex'。將對使用的字串hello 進行十六進位制編碼解碼。執行上述程式將生成以下輸出:
48656c6c6f
示例
在這個例子中,讓我們使用起始和結束偏移值來解碼字串。
使用起始/結束偏移值時,將返回解碼字串的一部分。
const buffer = Buffer.from('Hello World');
console.log(buffer.toString('hex', 2, 6));
輸出
6c6c6f20
示例
在這個例子中,我們將使用 Buffer.alloc() 並用一個值填充它。
const buffer = Buffer.alloc(10);
buffer.fill('H');
console.log(buffer.toString('hex'));
輸出
48484848484848484848
nodejs_buffer_module.htm
廣告
