欢迎来到代码驿站!

NodeJS

当前位置:首页 > 脚本语言 > NodeJS

Node.js API详解之 string_decoder用法实例分析

时间:2021-07-04 09:26:43|栏目:NodeJS|点击:

本文实例讲述了Node.js API详解之 string_decoder用法。分享给大家供大家参考,具体如下:

string_decoder 模块提供了一个 API,用于把 Buffer 对象解码成字符串。

对于参数末尾不完整的多字节字符,string_decoder会将其保存在内部的buffer中,当再次解码时,补充到参数开头。

通过 const { StringDecoder } = require(‘string_decoder'); 的方式引用string_decoder模块。

目录:

  • new StringDecoder([encoding])
  • stringDecoder.write(buffer)
  • stringDecoder.end([buffer])

new StringDecoder([encoding])

说明:

创建一个新的StringDecoder实例,可传递encoding参数作为字符编码格式,默认为'utf8′

stringDecoder.write(buffer)

说明:

返回一个解码后的字符串,并确保返回的字符串不包含残缺的多字节字符,残缺的多字节字符会被保存在一个内部的 buffer 中,
用于下次调用 stringDecoder.write() 或 stringDecoder.end()。
buffer:待解码的Buffer

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16进制小于0x80属于单字节
let outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoqiang
 
//字符的16进制大于0x80属于双字节
outString = decoder.write(Buffer.from([0xC2, 0xA2]));
 
console.log(outString);
//¢
 
//单双字节混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//¢
 
//单双字节混合,置于中间
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71,       0xC2, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoq?iang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//?
 
//单双字节混合,置于开始
outString = decoder.write(Buffer.from([0xC2,     0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//?xiaoqiang
 
outString = decoder.write(Buffer.from([0xA2]));
 
console.log(outString);
//?
 
//单双字节混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.write(Buffer.from([0x78,0xA2]));
 
console.log(outString);
//?x?

stringDecoder.end([buffer])

说明:

以字符串的形式返回内部 buffer 中剩余的字节,残缺的字节会被替换成符合字符编码的字符
如果提供了 buffer 参数,则在返回剩余字节之前会再执行一次 stringDecoder.write()

demo:

const decoder = new StringDecoder('utf8');
 
//字符的16进制小于0x80属于单字节
let outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end();
 
console.log(outString);
//
 
//单双字节混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end(Buffer.from([0xA2]));
 
console.log(outString);
//¢
 
//单双字节混合,置于末尾
outString = decoder.write(Buffer.from([0x78, 0x69, 0x61, 0x6f, 0x71, 0x69, 0x61, 0x6e, 0x67,       0xC2]));
 
console.log(outString);
//xiaoqiang
 
outString = decoder.end();
 
console.log(outString);
//?

希望本文所述对大家node.js程序设计有所帮助。

上一篇:node 版本切换的实现

栏    目:NodeJS

下一篇:node.js express框架实现文件上传与下载功能实例详解

本文标题:Node.js API详解之 string_decoder用法实例分析

本文地址:http://www.codeinn.net/misctech/152576.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有