Skip to content
aokihu edited this page Apr 7, 2022 · 6 revisions

Quick-Struct is a convert between binary and JavaScript Object.

You can easily define data structures in the same overall style as in C. At the same time, considering the differences between different computer architectures and operating systems, we choose to use Rust type names instead of C language type keywords.

Sample

struct {
   u8 a;
   u8 b;
  u16 c[8];
 char message[32];
  u32 size;
 char address[$size];
}

Quick-Struct supports automatic parsing of fixed-length arrays and strings, and can even automatically parse non-fixed-length arrays and strings. Just replace the fixed length with the field name that stores the length information, all of which are automatic

Basic usage

You can use qs template string function to create a new QStruct instance.

Sample - qs (template string function)

import { qs } from "quick-struct";

const struct = qs`
    struct {
        u8 a;
    }
`;

// Create a UInt8 array
const buffer = new UInt8Aray([12]);

// Decode buffer
const result = struct.decode(buffer).toJson();

// Print decoded result
console.log(result.a); // print 12

You can also use QStruct to create instance.

Sample - QStruct constructor

import { QStruct } from "quick-struct";

const structDescriptor = `
    struct {
        u8 a;
    }
`;

const struct = new QStruct(structDescriptor);

// Create a UInt8 array
const buffer = new UInt8Aray([12]);

// Decode buffer
const result = struct.decode(buffer).toJson();

// Print decoded result
console.log(result.a); // print 12
Clone this wiki locally