If your lengths aren't necessarily multiples of 8,
my @fields;
my $bits = unpack('B*', $input);
while ($bits) {
die if length($bits) < 8;
my $num_bits = unpack('C', pack('B*', substr($bits, 0, 8, '')));
my $field = substr($bits, 0, $num_bits, '');
push @fields, $field;
}
You can probably do this more efficiently using bit twiddling, but this is simpler.
If you want the result as a number rather than a string of "1"s and "0"s, you can use the following:
use Config qw( );
use constant UV_BITS => $Config::Config{uvsize} * 8;
die if $num_bits > UV_BITS;
push @fields, pack('B*', substr(("0" x UV_BITS).$field, -UV_BITS));
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.