Rob Dixon schreef:

> my $binary = unpack 'b*', join '', map chr hex, @hex;
>
> But this is a beginners' group :)

No problem if you explain some of the details:


(*1*) my @temp1 = map chr hex, @hex;


This achieves about the same:

  my @temp1;

  foreach my $elm (@hex)
  {
    push @temp1, chr( hex( $elm ) );
  }

or more compact:

  my @temp1; push @temp1, chr(hex($_)) for @hex;

or even more compact:

  my @temp1; push @temp1, chr hex for @hex;

It creates and fills an array (intermediate in the original expression,
named @temp1 here for clarity), in which each element is a single
character (not necessarily limited to \x00-\xFF).
See `perldoc -f hex` and `perldoc -f chr` (and of course `perldoc -f
map`).

Test-1:
  perl -wle "@data=qw(78 FF 7A); print chr(hex($_)) for @data"
(Windows)
  perl -wle "@data=qw(78 FF 7A); print chr(hex(\$_)) for @data" (*ix)
  perl -wle '@data=qw(78 FF 7A); print chr(hex($_)) for @data'  (*ix)
  perl -wle "@data=qw(78 FF 7A); print chr hex for @data"       (both)


(*2*) my $temp2 = join '', @step1

This just concatenates all ellements of @step1 (which are all a single
character) into a string.

Test-2:
  perl -wle "print join q//, (1, q/ZX/, 2)"

(I wrote the '' as q//, to make this work in most shells.)


(*3*) my $binary = unpack 'b*', $temp2 ;

This creates a binary representation of the string of characters, see
`perldoc -f pack` for details about the 'b' conversion.

Test-3:
  perl -wle "print unpack q/b8/, chr(0x51)"


-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to