in reply to Handling bits in Perl
Remember that you can use successive unpack()s to pick things apart and leave some, as in
use strict; my $str = '567'; my ($ch, $rest); ($ch, $rest) = unpack('a a*', $str); while (length $ch) { my $high_nib= ord($ch)>>4; my $low_nib = ord($ch) & 15; my ($high_bit,$case,$alpha,$rangel,$bit3,$bit2,$bit1,$bit0) = split '', unpack('B*',$ch); my @bits = split '', unpack('B*',$ch); print "$ch == @bits ... "; printf "%d, %d, %d, %d : %d/%d\n", $high_bit,$case,$alpha,$rangel,$high_nib,$low_nib; ($ch, $rest) = unpack('a a*', $rest); } # prints # 5 == 0 0 1 1 0 1 0 1 ... 0, 0, 1, 1 : 3/5 # 6 == 0 0 1 1 0 1 1 0 ... 0, 0, 1, 1 : 3/6 # 7 == 0 0 1 1 0 1 1 1 ... 0, 0, 1, 1 : 3/7
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Handling bits in Perl
by Cuhulain (Beadle) on Aug 05, 2006 at 18:14 UTC |