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])));输出
€
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP