binstruct 0.2.0
binstruct: ^0.2.0 copied to clipboard
A binary struct parser/builder similar to Python's construct
example/main.dart
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
}