Node.js - StringDecoder

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

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

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

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

来源:LearnFk无涯教程网

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

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

技术教程推荐

邱岳的产品手记 -〔邱岳〕

深入浅出云计算 -〔何恺铎〕

跟月影学可视化 -〔月影〕

乔新亮的CTO成长复盘 -〔乔新亮〕

全链路压测实战30讲 -〔高楼〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

B端产品经理入门课 -〔董小圣〕

云时代的JVM原理与实战 -〔康杨〕

Rust 语言从入门到实战 -〔唐刚〕

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