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

Re: [FWP] binary to ascii to binary



>>   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;
>>   }

vec($byte,0,8) treats $byte like a bit vector.  That is, it allows you to
modify $byte at a binary level.

  vec($A,0,8) = 65;  # this is ord('A'), the ASCII value for 'A'
  print $A;  # the letter 'A'

I merely set the bit vector [ummm]

OOPS!

That was TOTALLY unneeded.  I'll rewrite that function:

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

There.  Since $byte was just being the value of $_, I'll not use it at
all, and just pack the character as a binary string.  That was silly of me
before.

As for the wantarray stuff, it's like saying:

  if (the user wants a list of return values) {
    return @bytes;
  }
  else {
    return join " ", @bytes;  # return the elements with spaces in between
  }

>>       vec(my($byte), 0, 8) = ord;
>>       push @bytes, unpack "B8", $byte;

>I honestly don't know.

Honestly not needed anymore. :)

-- 
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