Node.js - StringDecoder

Node.js - StringDecoder 首页 / Node.js入门教程 / Node.js - StringDecoder

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

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

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

StringDecoder 方法

StringDecoder类只有两个方法。

方法描述
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

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

技术教程推荐

Service Mesh实践指南 -〔周晶〕

软件测试52讲 -〔茹炳晟〕

SQL必知必会 -〔陈旸〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

DevOps实战笔记 -〔石雪峰〕

检索技术核心20讲 -〔陈东〕

互联网人的英语私教课 -〔陈亦峰〕

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

云原生基础架构实战课 -〔潘野〕

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