# Hell of a time extracting bits from a vector

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I am generating a SHA1 digest that I want to use for some values.

I want to take the digest that is generated at extact a given number
of bits, in sequence.

SHA1 generates 160 bits.

I would like to partition that 160 bits into an array storing the
value of those bits

for instance (in short form using only 10 bits grabbing 2 at a time)
lets say I have:

1010010101

and I am grabbing pairs I need (from least to most):

@somearray

\$somearray[0] = 1 (01)
\$somearray[1] = 1 (01)
\$somearray[2] = 1 (01)
\$somearray[3] = 2 (10)
\$somearray[4] = 2 (10)

I I tried using vec (going back to the full 160 bits) but it fails
miserably. Here a sample

Using vec(\$digest,\$loop*8,8) (Grabbing 8 bits)

I would get
0101010101
1101011010
100101001010101101
010110110101011101
...

The lengths would be wrong (doing an unpack "b*" on the array that was
holding the result. Sometimes I would get 8, other 16 and the number
even changes running the same data!)

I even tried using the Binary::Vector with a Chunk_Read and got
similar results.

This should be this hard to just extract X bits from a Y*X offset
where Y is the loop iteration.

What am I missing?

## Re: Hell of a time extracting bits from a vector

use Digest::SHA1  qw(sha1);

unpack '(a)*', unpack 'B*', sha1( \$data );

my @somearray = unpack '(a2)*', unpack 'B*', sha1( \$data );

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

## Re: Hell of a time extracting bits from a vector

Exactly what I was looking for with one exception, I don't want to
store the ASCII in the array but rather the actual integer value.

my @somearray = unpack '(a2)*', unpack 'B*', sha1( \$data );

Works perfect but I tried changing the a2 to N or n but that fails
miserable. I figure I have to append an bin to int of some sort to the
front of the unpacks but so far I have been unsuccessful. Any
suggestions?

## Re: Hell of a time extracting bits from a vector

The obvious way is to add

map { oct "0b\$_" }

to the front; there may be a cleverer way with pack, but if you don't
need speed that's nice and simple.

Ben

## Re: Hell of a time extracting bits from a vector

Worked like a charm, thank ya!

## Re: Hell of a time extracting bits from a vector

you are probably forgetting that you need
to pack to an integer or short before you
you unpack to the bit string, eg,

# perl -le 'print unpack "B*",136'
001100010011001100110110

vs.

# perl -le 'print unpack "B*",pack("S",136)'
0000000010001000

--
Charles DeRykus