https://github.com/streamich/typebase
C/C++ structs in Node.js
https://github.com/streamich/typebase
Last synced: 11 months ago
JSON representation
C/C++ structs in Node.js
- Host: GitHub
- URL: https://github.com/streamich/typebase
- Owner: streamich
- Created: 2016-05-15T20:10:00.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-15T13:22:48.000Z (over 8 years ago)
- Last Synced: 2025-08-09T17:57:12.683Z (11 months ago)
- Language: TypeScript
- Size: 444 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `struct` for Node.js
Read `docco` docs at [here](http://streamich.github.io/typebase/). `npm` package is [here](https://www.npmjs.com/package/typebase).
Consider a `C/C++` structure:
```c
struct address {
int port,
unsigned char ip[4],
}
```
Define the same binary `struct` in JavaScript and pack/unpack data to `Buffer`:
```js
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);
```