Node.jsAPI中怎么使用string_decoder

这篇文章主要为大家展示了Node.js API中怎么使用string_decoder,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站建设、成都网站制作收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10余年的创新互联网站建设公司。

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 API中怎么使用string_decoder的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

网站名称:Node.jsAPI中怎么使用string_decoder
转载来源:https://www.cdcxhl.com/article32/pgdepc.html

成都网站建设公司_创新互联,为您提供云服务器品牌网站设计App设计定制开发网站策划ChatGPT

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司