我知道有一个名为slice()
的函数,但我正在寻找splice()
,但该函数不存在,我将如何以其他方式进行操作?
var buffer = new Buffer("090001060001020304090000060001020304", "hex");
var packetLength = buffer.readUInt16LE(0)
console.log('before slice buffer = ' + buffer.toString('hex'))
buffer = buffer.slice(0, packetLength)
console.log('after slice buffer = ' + buffer.toString('hex'))
我得到的输出是
before slice buffer = 090001060001020304090000060001020304
after slice buffer = 090001060001020304
但是我需要获得
before slice buffer = 090001060001020304090000060001020304
after slice buffer = 090000060001020304
应该删除前9个字节,然后将其移到最前面的字节?
答案 0 :(得分:1)
只需将缓冲区的起点更改为偏移9而不是从0开始
let newBuffer = buffer.slice(9).toString('hex')
答案 1 :(得分:0)
解决了..
console.log('before slice buffer = ' + buffer.toString('hex'))
var newBuffer = new Buffer(buffer.length - packetLength);
buffer.copy(newBuffer, 0, packetLength, packetLength + buffer.length);
console.log('after slice buffer = ' + newBuffer.toString('hex'))
before slice buffer = 090001060001020304090000060001020304
after slice buffer = 090000060001020304