I'm relatively inexperienced with Perl, but my question is about the decompress function when getting bits for a numeric value. For instance:
my $bits = unpack("b*", 1);
print $bits;
This results in a print of 10001100, which is 140 in decimal. In reverse order, it is 49 in decimal value. Any other values that I tried seem to give the wrong bits.
However, when I run $ bits through the package, it again produces 1. Is there something missing here?
I seem to have come to conclusions when I decided that my problem was resolved. Perhaps I should briefly explain what I'm trying to do.
I need to convert an integer value that can be up to 24 bits in size (the point is that it can be more than one byte) into a bit string. This can be achieved with unpack and pack, as suggested by @ikegami, but I also need to find a way to convert this string to the original integer (not a string representation).
As I mentioned, I'm relatively inexperienced with Perl, and I try without success.
I found what seems like an optimal solution:
my $bits = sprintf("%032b", $num);
print "$bits\n";
my $orig = unpack("N", pack("B32", substr("0" x 32 . $bits, -32)));
print "$orig\n";
source
share