http://www.perlmonks.org?node_id=99218

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: bit string
by I0 (Priest) on Jul 24, 2001 at 07:39 UTC
    print unpack"B8", pack "C",$a;
Re: bit string
by Zaxo (Archbishop) on Jul 24, 2001 at 07:06 UTC

    Binary printf formats aren't supported in earlier versions. From v5.6.0 perldoc perldelta:

    "Binary numbers supported

     Binary numbers are now supported as literals, in s?printf formats, and `oct()':

         $answer = 0b101010;
         printf "The answer is: %b\n", oct("0b101010");"

    Update: forgot to put in the meat.
    As a string:

    $ perl -e '$c = 12;print reverse map {vec $c,$_,1} 0..8*length($c)-1; +' 0011001000110001

    As a number:

    $ perl -e '$c = 12;print reverse map {$c&1<<$_?1:0} 0..8;' 000001100

    After Compline,
    Zaxo