An open API service indexing awesome lists of open source software.

https://github.com/agbianchessi/js-struct

C-like Structs for JavaScript.
https://github.com/agbianchessi/js-struct

binary c data struct

Last synced: 2 days ago
JSON representation

C-like Structs for JavaScript.

Awesome Lists containing this project

README

          

JS-Struct-AGB
======
C-like Structs for JavaScript.
A browser porting of the Node.js [Struct library](https://github.com/xdenser/node-struct).

Install
============

```
npm install js-struct-agb
```

Example
=======

Define some structure:

import Struct from 'js-struct-agb';

var Person = Struct()
.chars('firstName',10)
.chars('lastName',10)
.array('items',3,'chars',10)
.word16Sle('balance'),
People = Struct()
.word8('presentCount')
.array('list',2,Person);

Now allocate buffer for it

People.allocate();
var buf = People.buffer();

Clear buffer to see how it will change later:

var buf = People.buffer();
for (var i = 0; i < buf.length ; i++) {
buf[i] = 0;
}
console.log(buf);

Output:



Now you can access memory as defined binary structure with `fields` property in a handy manner.

var proxy = People.fields;
proxy.presentCount = 2;
console.log(buf);

Output:



And so on

proxy.list[0].firstName = 'John';
console.log(buf);

Output: