[Dub-pm] Quick pack question.

Mcnamara John John.Mcnamara at snamprogetti.eni.it
Wed Oct 13 03:43:47 CDT 2004

> How do I take a string formated like so, bitwise-speaking;
> 11111112 22222233 33333444 55556666 ...
> (that is, the first seven bits of the first octet correspond to the
> first character, the last bit of the first octet concatenated with
> the first six of the next correspond to the second character ...)

Here is one way to do it (if I have understood correctly):

    #!/usr/bin/perl -wl

    my $str = sprintf "%032b", 123456789;
    print $str, "\n";

    my $offset = 0;
    while ($offset < length $str) {
        my $septet = unpack 'C', pack 'B7',
                     substr ($str, $offset, 7);
           $septet >>= 1;

        printf "%07b %d\n", ($septet) x 2;
        $offset += 7;


    0000011 3
    1010110 86
    1111001 121
    1010001 81
    0101000 40

It should also be possible to generate a longer "B7" template and have
only one call to pack(). I'll post an example of that if the above is
close to what you want.


*************************E-MAIL CONFIDENTIALITY FOOTER**********************
This message may contain confidential information and must not be copied,
disclosed or used by anybody other than the intended recipient.
If you have received this message in error, please notify us writing
to postmaster at snamprogetti.eni.it and delete the message and any copies of it.
Thank you for your assistance.

More information about the Dublin-pm mailing list