2

I know there is a function called slice() but I am looking for splice() and that function doesn't exist how would I go about doing it some other way?

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'))

the output I get is

before slice buffer = 090001060001020304090000060001020304
after slice buffer = 090001060001020304

But i need to get

before slice buffer = 090001060001020304090000060001020304
after slice buffer = 090000060001020304

the front 9 bytes should get removed and the bytes after it should be moved to the front how do I do this easy way?

2 Answers 2

3

Just change the starting point of your buffer to be offset by 9 instead of starting from 0

let newBuffer = buffer.slice(9).toString('hex')

Sign up to request clarification or add additional context in comments.

1 Comment

cool thats even better then have I have done, can't believe I overlooked this
0

Solved it..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.