Perl & Binary Data

Peter Scott Peter at PSDT.com
Tue Dec 17 13:31:43 CST 2002


abez at abez.ca wrote:
 > > > >while ($len = sysread($fd,$buffer,$size)) {
 > > > >         my @o = unpack("s*",$buffer);
 > > > >         foreach (@o) {
 > > > >                 $_/=2;
 > > > >         }
 > > > >         syswrite($fd,pack("s*",$buffer,$size);
 > > > >}
 > > > >
 > > > >This seems really slow for datarates of 178kb/s

This seems to work:

my $data = "\x10\x02\x80\xff";  # Test data

for (my $i = 0; $i < length($data); $i += 2) {
   my $x = vec($data, $i >> 1, 16);
   vec($data, $i >> 1, 16) = ($x >> 1) | ($x & 0b10000000);
}

Assuming twos complement and that the top bit being set indicates it's 
negative... I'm rusty there.  May be endian issues.

It would be a lot easier if << and >> worked on bit strings.  I don't 
know why they don't.  I've asked on comp.lang.perl.moderated.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/




More information about the Victoria-pm mailing list