Reputation: 11
Here is the example data stream (in buffer). The 0x81
is the end of each block, need to process the logic until next 0x81
.
00:15:04:00:00:30:04:54:52:55:45:05:14:00:00:00:04:4b:30:01:00:64:81:00:14:04:00:00:10:03:42:57:47:03:0c:00:14:00:04:4b:31:01:01:f4:81:00:1a:04:00:00:14:07:44:43:4f:4e:2d:57:32:00:80:00:03:00:04:4b:33:02:07:08:20:08:81:00:15:04:00:00:20:04:49:52:50:43:01:ba:00:00:00:04:4b:34:01:00:64:81:00:15:04:00:00:00:04:42:44:4d:53:07:e4:00:1e:00:04:4b:35:01:03:20:81
I need to process the complete streams but don't know how to handle with uncertain length of each block.
Upvotes: 0
Views: 186
Reputation: 3874
You can try with something like :
var bytes = "00:15:04:00:00:81:00:01:81:51:81".split(":");
var buffer = new Buffer(bytes);
function getNextBlock(buffer, pos) {
if (pos >= buffer.length)
return false;
var block = [];
while (pos < buffer.length && buffer[pos] != "81")
block.push(buffer[pos++]);
return block.length ? block : false;
}
var block;
var pos = 0;
while(block = getNextBlock(buffer, pos)) {
console.log(block);
pos += 1 + block.length;
}
Upvotes: 1