Node.js – stringDecoder.end() 方法


stringDecoder.end() 方法會返回作為字串留在內部緩衝區的任何剩餘輸入。左不完整的位元組,表示 UTF-8 和 UTF-16 字元,用適當的替換字元替換為字元編碼。

如果提供了任何緩衝區引數,則在返回剩餘輸入之前呼叫 **StringDecoder.write()** 方法。一旦呼叫 **end()** 方法,stringDecoder 即可重複用於獲取新的輸入。

語法

stringDecoder.end( [buffer] )

引數

  • 緩衝區 - 此引數獲取要解碼的位元組作為輸入。它可以將緩衝區、TypedArray 或 DataView 作為輸入引數。

示例 1

使用名稱“end.js”建立檔案,然後複製以下程式碼。建立檔案後,使用命令“node end.js”以按以下示例所示執行此程式碼

// stringDecoder.end() method Demo Example

// Importing the string_decoder module
const { StringDecoder } = require("string_decoder");

// Defining the decoder type
const decoder = new StringDecoder("utf-8");

// Converting text to buffer
const text = Buffer.from("TutorialsPoint", "utf-8");

// Getting text from the buffer using end() method
let decoded_text = decoder.end(text);

// Printing the decoded text
console.log("Decoded Text:", decoded_text);

輸出

Decoded Text: TutorialsPoint

示例 2

// stringDecoder.end() method Demo Example

// Importing the string_decoder module
const { StringDecoder } = require("string_decoder");

// Defining the decoder type
const decoder = new StringDecoder("utf-8");

// Euro Symbol: [0xE2, 0x82, 0xAC]
console.log("Decoding the Euro Symbol:");

// Writing the euro values in decoder
decoder.write(Buffer.from([0xE2]));
decoder.write(Buffer.from([0x82]));

// Printing the symbol using end() method
console.log(decoder.end(Buffer.from([0xAC])));

輸出

更新於: 04-12-2021

121 次瀏覽

職業開啟

完成課程獲取認證

開始學習
廣告
© . All rights reserved.