FW: [Dub-pm] Quick pack question.

Mcnamara John John.Mcnamara at snamprogetti.eni.it
Wed Oct 13 04:07:36 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 ...)


Or without pack:

    #!/usr/bin/perl -wl

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


    my $offset = 0;

    while ($offset < length $str) {
        my $septet = oct 'b' . substr($str, $offset, 7);

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


    __END__
    Prints:

    00000111010110111100110100010101

    0000011 3
    1010110 86
    1111001 121
    1010001 81
    0000101 5


This handles the trailing bits less than 7 differently from the other
example. Bug or feature, you decide. ;-)

John.
--





























*************************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