本文实例讲述了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"font-size: medium">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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]