in reply to unpacking 6-bit values

my $buf; my @out = (); while ( length( $bigstring )) { $buf = ''; $buf = $buf . chop $bigstring for (0..2); for (0..3) { push @out, ($buf & 64); # update: Perl didn't accept this bit +op $buf >>= 6; # but is happy with this one } }

update: Grr if only all perl bitwise operators worked on strings! Pity - it would be pointless fixing this by unpacking the "offending" byte because I was deliberately trying to avoid any unpacking in this attempt at a quickie solution.

One world, one people

Replies are listed 'Best First'.
Re^2: unpacking 6-bit values
by BrowserUk (Patriarch) on Dec 16, 2010 at 15:07 UTC
    #! perl -slw use strict; sub doit { my $in = shift; my $buf; my @out = (); while( length( $in ) ) { $buf = ''; $buf = $buf . chop $in for (0..2); for( 0 .. 3 ) { push @out, ($buf & 64); $buf >>= 6; } } return @out } our $packed = pack 'C*', qw[ 00 16 131 16 81 135 32 146 139 48 211 143 65 20 147 81 85 151 97 1 +50 155 113 215 159 ]; print join ' ', map sprintf( "%02d", $_ ), doit( $packed ); __END__ C:\test>junk10 Argument "ƒÎq" isn't numeric in bitwise and (&) at C:\test\junk10.pl l +ine 12. Argument "øûa" isn't numeric in bitwise and (&) at C:\test\junk10.pl l +ine 12. Argument "ùUQ" isn't numeric in bitwise and (&) at C:\test\junk10.pl l +ine 12. Argument "ô^TA" isn't numeric in bitwise and (&) at C:\test\junk10.pl +line 12. Argument "M-^OË0" isn't numeric in bitwise and (&) at C:\test\junk10.p +l line 12. Argument "ïÆ " isn't numeric in bitwise and (&) at C:\test\junk10.pl l +ine 12. Argument "çQ^P" isn't numeric in bitwise and (&) at C:\test\junk10.pl +line 12. Argument "â^P\0" isn't numeric in bitwise and (&) at C:\test\junk10.pl + line 12. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0 +0 00 00 00 00 00 00 00 00

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.