Node.jsStringDecoder

Node.jsStringDecoder 首页 / Node.js入门教程 / Node.jsStringDecoder

Node.js StringDecoder用于将缓冲区解码为字符串。它类似于buffer.toString(),但为UTF提供了额外的支持。

您需要使用require('string_decoder')来使用StringDecoder模块。

const StringDecoder = require('string_decoder').StringDecoder;

StringDecoder 方法

StringDecoder类只有两个方法。

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-stringdecoder.html

来源:LearnFk无涯教程网

方法描述
decoder.write(buffer)它用于返回解码后的字符串。
decoder.end()如果缓冲区中有剩余字节,则用于返回尾随字节。

StringDecoder 示例

让无涯教程来看一个Node.js StringDecoder的简单示例。

文件:stringdecoder_example1.js.

const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');

const buf1 = new Buffer('this is a test');
console.log(decoder.write(buf1));//prints: this is a test

const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex');
console.log(decoder.write(buf2));//prints: this is a test

const buf3 = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);
console.log(decoder.write(buf3));//prints: buffer
Node.js stringdecoder example 1

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

云原生架构与GitOps实战 -〔王炜〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

自动化测试高手课 -〔柳胜〕

玩转Vue 3全家桶 -〔大圣〕

Go 语言项目开发实战 -〔孔令飞〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

NLP实战高手课 -〔王然〕

Flutter核心技术与实战 -〔陈航〕

玩转Git三剑客 -〔苏玲〕

好记忆不如烂笔头。留下您的足迹吧 :)