Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

split binary data into array or classes in node.js

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?

Problem courtesy of: LordZardeck

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 
Solution courtesy of: fent

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

split binary data into array or classes in node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×