stringDecoder.write()方法用于从给定的Buffer,TypedArray或DataView返回解码后的字符串。此方法还确保从返回的字符串中忽略缓冲区末尾的任何不完整的多字节字符。这些字符存储在内部缓冲区中,以供下次调用stringDecoder.write()
或者stringDecoder.end()
方法。
用法:
stringDecoder.write( buffer )
参数:此方法接受上述和以下描述的单个参数:
- buffer:它是一个Buffer,TypedArray或DataView,其中包含必须解码的字节。
返回值:它返回从给定缓冲区解码的字符串。
以下示例程序旨在说明Node.js中的stringDecoder.write()方法:
范例1:
// Node.js program to demonstrate the
// stringDecoder.write() Method
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
// Create a new instance of the decoder
const decoder = new StringDecoder("utf-8");
const text_one = Buffer.from("GeeksforGeeks", "utf-8");
let decoded_text = decoder.write(text_one);
console.log("Decoded Text:", decoded_text);
输出:
Decoded Text:GeeksforGeeks
范例2:
// Node.js program to demonstrate the
// stringDecoder.write() Method
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
// Create a new instance of the decoder
const decoder = new StringDecoder("utf-8");
// Decoding text using hex from buffer
const hex_text = new Buffer.from(
"4765656B73666F724765656B73", "hex");
let decoded_hex_text = decoder.write(hex_text);
console.log("Decoded Text Hex:", decoded_hex_text);
// Decoding text using base64 from buffer
const base64_text = new Buffer.from(
"R2Vla3Nmb3JHZWVrcw==", "base64");
let decoded_base64_text = decoder.write(base64_text);
console.log("Decoded Text Base64:", decoded_base64_text);
// Decoding the cent symbol from buffer
const cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol:", cent_symbol_out);
输出:
Decoded Text Hex:GeeksforGeeks Decoded Text Base64:GeeksforGeeks Cent Symbol:¢
参考: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_write_buffer
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
- Node.js GM threshold()用法及代码示例
- Node.js GM segment()用法及代码示例
- Node.js GM resize()用法及代码示例
- Node.js GM raise()用法及代码示例
- Node.js GM quality()用法及代码示例
- Node.js GM transparent()用法及代码示例
- Node.js GM roll()用法及代码示例
- Node.js GM solarize()用法及代码示例
- Node.js GM spread()用法及代码示例
- Node.js GM motionBlur()用法及代码示例
- Node.js GM operator()用法及代码示例
- Node.js GM shave()用法及代码示例
- Node.js GM scale()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | stringDecoder.write() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。