[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] binary to ascii to binary



>The source file is available at www.chebucto.ns.ca/~aw096/bin2asc.pl
>Take a look and let me know what you think, what I did that was stupid
>and/or counterproductive. Also, if you think it's neat.

  #!/usr/bin/perl -l

  chomp($string = <STDIN>);
  $asBinary = asc2bin($string);
  $asASCII  = bin2asc($asBinary);

  sub asc2bin {
    my ($str) = @_;
    my @bytes;
    for (split //, $string) {
      vec(my($byte), 0, 8) = ord;
      push @bytes, unpack "B8", $byte;
    }
    return wantarray ? @bytes : join " ", @bytes;
  }


  sub bin2asc {
    @_ = split ' ', $_[0] if @_ == 1;
    my @chars;
    for (@_) {
      my $asc = unpack "C", pack "B8", $_;
      push @chars, chr($asc);
    }
    return wantarray ? @chars : join "", @chars;
  }

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy@pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe