buffer([字符串名],[写到buffer的第几位],[写多少个],[编码])
第二个参数不是字符串str的起始位置
var str='panwen'; var bf=new Buffer(6); bf.write(str,0);//第二个参数是写到buffer中的第几位 console.log(bf);//<Buffer 00 70 61 6e 77 65> for(var i=0;i<bf.length;i++){ console.log(String.fromCharCode(bf[i])); }// p a n w e n bf.write(str,1);//第二个参数是写到buffer中的第几位 console.log(bf);//<Buffer 00 70 61 6e 77 65> for(var i=0;i<bf.length;i++){ console.log(String.fromCharCode(bf[i])); }//p p p a n w e bf.write(str,2);//第二个参数写到buffer中的第几位,而不是str的起始位置 console.log(bf);//<Buffer 00 70 70 61 6e 77> for(var i=0;i<bf.length;i++){ console.log(String.fromCharCode(bf[i])); }//p p p a n w
本文介绍了在Node.js中如何使用Buffer对象将字符串写入指定位置,详细解释了Buffer的写入方法及参数含义,包括字符串起始位置和编码的选择。
779

被折叠的 条评论
为什么被折叠?



