binstruct 0.1.3
binstruct: ^0.1.3 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 4-byte float ('c')
final struct = BinStruct([
Uint8('a'),
Int16('b'),
Float32('c'),
]);
// Build binary data from a map
final bytes = struct.build({
'a': 42,
'b': -1234,
'c': 3.14,
});
print('Binary bytes: $bytes'); // Prints [42, 251, 46, 64, 72, 245, 195]
// Parse the bytes back to a Dart map
final parsed = struct.parse(bytes);
print('Parsed data: $parsed'); // Prints {a: 42, b: -1234, c: 3.140000...}
// Validate the parsed values
assert(parsed['a'] == 42);
assert(parsed['b'] == -1234);
assert(num.parse(parsed['c'].toStringAsFixed(2)) == 3.14);
}