執行環境套件管理器打包器測試執行器程式碼檢查格式化指南參考部落格意見回饋

將 Buffer 轉換為字串

Buffer 類提供了一個 .toString() 方法,可將 Buffer 轉換為字串。

TypeScript
const buf = Buffer.from("hello");
const str = buf.toString();
// => "hello"

你可以選擇指定編碼和位元組範圍。

TypeScript
const buf = Buffer.from("hello world!");
const str = buf.toString("utf8", 0, 5);
// => "hello"

請參閱二進位制資料。