# use 'n' instead of 'S' to support big- AND little-endian systems
# thanks to posts on perlmonks for hints!
sub custom_pack {
unpack 'n', pack 'B16', sprintf '%02b%06b%03b%05b', @_;
}
sub custom_unpack {
map oct "0b$_", unpack 'a2a6a3a5', unpack 'B16', pack 'n', @_;
}
print sprintf('%X', custom_pack(2,42,6,14)), "\n";
print join(',', custom_unpack(0xAACE)), "\n";
On Thu, 2005-01-13 at 14:59, Stephen A. Jarjoura wrote:
> Hello, I have a question about arbitrary bit packing functions.
>
> Store / Retrieve four (4) fields (2 bits, 6 bits, 3 bits, 5 bits) into 2 bytes
> Example:
> ## bit string: 10 101010 110 01110
> custom_pack(2,42,6,14) == \xAACE
> custom_unpack(\xAACE) == (2,42,6,14)
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm