nodejs将空终止的string添加到缓冲区

我正在尝试复制一个数据包。

这个包:

2C 00 65 00 03 00 00 00 00 00 00 00 42 4C 41 5A 45 00 00 00 00 00 00 00 00 42 4C 41 5A 45...... 

2c 00是数据包的大小…
65 00是数据包ID 101 …
03 00是数组中元素的数量…

现在这里出现我的问题, 42 4C 41 5A 45是一个string…该string正好有3个实例,如果它是完整的…但我的问题是它不是只是null终止它有00 00 00 00这些实例之间的空格。

我的代码:

 function channel_list(channels) { var packet = new SmartBuffer(); packet.writeUInt16LE(101); // response packet for list of channels packet.writeUInt16LE(channels.length) channels.forEach(function (key){ console.log(key); packet.writeStringNT(key); }); packet.writeUInt16LE(packet.length + 2, 0); console.log(packet.toBuffer()); } 

但是,我如何添加填充?

我正在使用这个包, https://github.com/JoshGlazebrook/smart-buffer/

智能缓冲区会跟踪您的位置,因此您不需要指定偏移量即可知道将数据插入到string的哪个位置。 你可以用你现有的代码做这样的事情:

 channels.forEach(function (key){ console.log(key); packet.writeString(key); // This is the string with no padding added. packet.writeUInt32BE(0); // Four 0x00's are added after the string itself. }); 

我假设你想要:42 4C 41 5A 45 00 00 00 00 42 4C 41 5A 45 00 00 00 00等

根据评论编辑:

没有build立任何你想做的事,但你可以做这样的事情:

 channels.forEach(function (key){ console.log(key); packet.writeString(key); for(var i = 0; i <= (9 - key.length); i++) packet.writeInt8(0); });