in reply to perl bitology - breaking into bytes

You could convert your 128 Bytes to a bit-String using regular expressions to match that.
something like
# Warning: Untested code ahead! # Enter at your own risk! read($file, $buf, 128); $bits= unpack("B*",$buf); # or "b*" depending on bit order do { if ($r= read($file, $byte, 1)) { $newbits=unpack("B*",$byte); # or "b*" depending on bit order $bits.=$newbits; } print "match!\n" if $bits=~ /^.{0,7}.{9}0{6}.{35}1{51}.{27}/; # ^ ^ ^ ^ ^ ^ # possible offset. 0-7 Bit ---/ | | | | | # match exactly 9 bits -------------/ | | | | # match Bits 9-14 as 0 -----------------/ | | | # match 35 Bits to advance to bit 50 -------/ | | # Bits 50-100 (51 Bits) must match 1 ------------/ | # to match the last 27 bits of your frame-------------/ $bits= substr($bits,8); } while ($r);
Not very efficient though.

Replies are listed 'Best First'.
Re: Re: perl bitology - breaking into bytes
by spurperl (Priest) on Oct 20, 2003 at 11:44 UTC
    The problem is deeper... My examples were just figurative, as a matter of fact, a frame is valid when certain single bits are 1, with no relations between them.

    There's a quite straightforward solution to the problem: read the file, unpack() it into a nice string of 1s and 0s, and work on it. Perl likes nice strings.

    However, this results in a peformance and memory problem. The file may get huge (100 MB), hence the string of 1s and 0s is 800 MB (each bit is represented by a char), which easily throws my PC out of memory.

    However, I don't see another way... Digging in bits directly, w/o unpacking them to a string is extremely difficult.

    I might just go for the string solutions, but will solve the memory problem by not having the whole string in memory, but only some buffer. Messy, but it should work, I hope.