首先,传入了<Buffer e4 bd a0 e5 a5>
,好
还差1个字节,此时,decoder.write(xx)
返回你
。
然后,再次调用decoder.write(Buffer.from([0xbd]))
,将剩余的1个字节传入,成功返回好
。
1 2 3 4 5 6 7 8 9 |
|
例子:decoder.end()时,字节数不完整的处理
decoder.end(buffer)
时,仅传入了好
的第1个字节,此时调用decoder.end()
,返回了?
,对应的buffer为<Buffer ef bf bd>
。
1 2 3 4 5 6 7 |
|
官方文档对于这种情况的解释是这样的(跟废话差不多),大约是约定俗成了,当utf8
码点无效时,替换成ef bf bd
。
Returns any remaining input stored in the internal buffer as a string. Bytes representing incomplete UTF-8 and UTF-16 characters will be replaced with substitution characters appropriate for the character encoding.
相关链接
你应该记住的一个UTF-8字符「EF BF BD」http://liudanking.com/golang/utf-8_replacement_character/
更多编程相关知识,请访问:编程视频!!
以上就是Nodejs中使用string_decoder模块将buffer转成string的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
nodejs中使用string_decoder模块将buffer转成string
更多相关阅读请进入《nodejs》频道 >>
Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者