split binary data into array or classes in node.js
Problem
How can I Split a buffer of Binary data in Node.js by a binary delimiter? For example, a socket data is sent with binary code with each field dilimited by \xb8. How can I split that into an array?
Better yet, is there some way to write a class or something that it can be loaded into? For example, each packet sends command-argument pairs delimited by \xb8. Is there anyway I can take a variable with binary data and break into multiple Command instances?
Solution
Read the Buffers documentation.
Iterate through each character in the buffer and create a new buffer whenever you encounter the specified character.
function splitBuffer(buf, delimiter) {
var arr = [], p = 0;
for (var i = 0, l = buf.length; i
Discussion
View additional discussion.