typebase
v1.0.1
Published
Package and unpackage binary structs
Downloads
34
Maintainers
Readme
struct
for Node.js
Read docco
docs at here. npm
package is here.
Consider a C/C++
structure:
struct address {
int port,
unsigned char ip[4],
}
Define the same binary struct
in JavaScript and pack/unpack data to Buffer
:
var t = require('typebase');
var address = t.Struct.define([
['port', t.i32],
['ip', t.List.define(t.ui8, 4)]
]);
var p = new t.Pointer(new Buffer(address.size), 0);
var host = {
port: 8080,
ip: [127, 0, 0, 1]
};
address.pack(p, host);
var unpacked = address.unpack(p);
console.log(unpacked);