title: binary structures in node.js
body: <p>I am writing a server, and through TCP I receive buffers. This buffers are a structure,</p>
  
  <p>So I C, usually it is done like:</p>
  
  <pre><code>typedef struct _ref_{
      uint8_t x;
      uint8_t *y;
      uint8_t z[16];
  }REF;
  </code></pre>
  
  <p>Unrelated to code above, my buffer could look like:</p>
  
  <pre><code>01 00 00 00 0A
  </code></pre>
  
  <p>Could be defined as: (little endian)</p>
  
  <pre><code>UINT16 UINT16 UINT8
  </code></pre>
  
  <p>So that would be</p>
  
  <pre><code>1, 0, 10
  </code></pre>
  
  <p>I would want to convert this into a json object like</p>
  
  <pre><code>UINT16 is X, UINT16 is Y, UINT8 is Z
  </code></pre>
  
  <p>Then result would be</p>
  
  <pre><code>x: 1, y: 0, z: 10
  </code></pre>
  
  <p>But how could it be done in node.js (not browser related)</p>
  
  <hr>
  
  <p>I saw <a href="https://github.com/rfw/restruct.js" rel="nofollow">restructjs</a> but it doesn't support strings that are null terminated, the strings are length specific.</p>
  
  <p>I also saw <a href="https://github.com/natevw/struct-fu" rel="nofollow">struct-fu</a> which is a bit more recent but still problems with null terminated strings.</p>