2.1.4 • Published 5 years ago

nstruct v2.1.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Introduction

nodejs struct like C Language

Installation

To install:

npm install nstruct

Example

var NStruct = require('nstruct');

var ns = new NStruct();

var buffer = Buffer.from([0x00, 0x01, 0x47, 0x6F, 0x64, 0x64, 0x01, 0xff]);

var template = {
  id: 'UInt16',
  name: 'Char[3]',
  rank: 'UInt8',
  pos: {x: 'UInt8',y: 'UInt8'}
};

var structInstance = ns.bufferToStruct(buffer, template);

console.log(structInstance);

var bufferInstance = ns.structToBuffer(structInstance, template);

console.log(bufferInstance);
	

Output: { id: 1, name: 'G', 'o', 'd' , rank: 100, pos: { x: 1, y: 255 } } <Buffer 00 01 47 6f 64 64 01 ff>

Basic Type

typebytes
UInt81
UInt162
UInt324
Int81
Int162
Int324
Char1
Float4
Double8

Array Type

Arrays are supported for all base types

eg: UInt82 Float4

Template

Basic organization of data structures,like

{ 
  id: 'UInt32',
  name: 'Char[3]',
  rank: 'Int8',
  pos: [{ x: 'UInt8', y:'UInt8' },{ x: 'UInt8', y: 'UInt8' }]
}

Function

sizeof(template) calculate bytes of template

offset(template, field) calculate field offset in template

bufferToStruct(buffer, template) convert to struct from buffer

structToBuffer(struct, template) convert to buffer from struct

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago