Node.js nodejs将以null结尾的字符串添加到缓冲区

Node.js nodejs将以null结尾的字符串添加到缓冲区,node.js,Node.js,我正在尝试复制一个数据包 此数据包: 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是数组中的元素数 现在我的问题来了,424c415a45是一个字符串。。。如果该数据包是完整的,则该数据包中正好有3个该字符串的实例。。。但我的问题是,它不仅以null结尾,而且在这些实例

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

此数据包:

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
是数组中的元素数

现在我的问题来了,
424c415a45
是一个字符串。。。如果该数据包是完整的,则该数据包中正好有3个该字符串的实例。。。但我的问题是,它不仅以null结尾,而且在这些实例之间有
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());
}
但是如何添加填充物呢


我正在使用此软件包,

智能缓冲区为您跟踪其位置,因此您无需指定偏移量即可知道在何处插入数据以填充字符串。您可以使用现有代码执行以下操作:

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 42 4C 41 5A 45 00 00 00等等

根据评论进行编辑:

没有内在的方式来做你想做的事,但你可以这样做:

channels.forEach(function (key){
    console.log(key);
    packet.writeString(key); 
    for(var i = 0; i <= (9 - key.length); i++)
        packet.writeInt8(0);
});
channels.forEach(功能(键){
控制台日志(键);
数据包写入(密钥);

对于(var i=0;我不能只向
forEach
循环添加几个
packet.writeInt16(0)
调用?@jibsales否,因为字符串的长度可能不同。@jibsales或者我可以检查长度并为每个缺少的字节添加writeInt16(0)来完成它?或者代替
packet.writestring()
您可以使用
packet.writeString(value,[offset])
@jibsales,但这会如何添加填充?是的,但如果键长为8个字符怎么办?应该只剩下一个00。字符串基于配置。长度限制为8个字符。例如,
42 00