binstruct 0.2.5 copy "binstruct: ^0.2.5" to clipboard
binstruct: ^0.2.5 copied to clipboard

A binary struct parser/builder similar to Python's construct

binstruct #

binstruct is a declarative binary parser and builder for Dart, inspired by Python's construct.

It allows you to define binary structures in Dart and easily parse or build binary data using Dart maps.

binstruct is currently in its infancy and does not have many features, but the package gets updated often with new stuff.

Features #

  • Declarative binary structure definitions
  • Support for:
    • Fixed-width integer types: Int8, Uint8, Int16, Uint16
    • Floating-point: Float32
    • Custom endianness (big or little)
    • Nested structs with StructField
  • Encode Dart maps to Uint8List
  • Decode Uint8List into Dart maps

Usage #

import 'package:binstruct/binstruct.dart';

void main() {
  // Define a binary struct with three fields:
  // - a 1-byte unsigned int ('a')
  // - a 2-byte signed int ('b')
  // - a binary struct with two fields ('c'):
  //   - a 1-byte unsigned int ('d')
  //   - a 1-byte unsigned int ('e')
  final struct = BinStruct([
    Uint8('a'),
    Int16('b'),
    StructField('c', BinStruct([
      Uint8('d'), 
      Uint8('e')
   ])),
  ]);

  // Build binary data from a map
  final bytes = struct.build({
    'a': 42,
    'b': -1234,
    'c': {'d': 0, 'e': 255}
  });

  print('Binary bytes: $bytes'); // Prints [42, 251, 46, 0, 255]

  // Parse the bytes back to a Dart map
  final parsed = struct.parse(bytes);

  print('Parsed data: $parsed'); // Prints {a: 42, b: -1234, c: {d: 0, e: 255}}

  // Validate the parsed values
  print(parsed['a'] == 42); // Prints true
  print(parsed['b'] == -1234); // Prints true
  print(parsed['c']['d'] == 0); // Prints true
  print(parsed['c']['e'] == 255); // Prints true
}

License #

MIT License. See LICENSE for details.

0
likes
145
points
31
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A binary struct parser/builder similar to Python's construct

License

MIT (license)

More

Packages that depend on binstruct