Expand description
A library for packing and unpacking binary data in Rust using format strings similar to Python’s struct module.
§Examples
use bunpack::{pack, unpack};
let data: Vec<u8> = pack!("i", 1234);
let value: i32 = unpack!("i", &data);
assert_eq!(value, 1234);§Format Specifiers
§Byte Order
Format specifiers can begin with an optional character to specify the byte order:
| Specifier | Description |
|---|---|
@ | Native byte order |
< | Little-endian |
> | Big-endian |
§Type Specifiers
Following the optional byte order, format specifiers can include one or more type specifiers:
| Specifier | Rust Type | Size in bytes |
|---|---|---|
c | char | 4 |
b | i8 | 1 |
B | u8 | 1 |
? | bool | 1 |
h | i16 | 2 |
H | u16 | 2 |
i | i32 | 4 |
I | u32 | 4 |
q | i64 | 8 |
Q | u64 | 8 |
o | i128 | 16 |
O | u128 | 16 |
n | isize | native |
N | usize | native |
f | f32 | 4 |
d | f64 | 8 |
s | &str | pack only: length of string |
p | &[u8] | pack only: length of byte array |
P | *const () | native |