Dr.Ruud wrote:
Rob Dixon schreef:

use strict;
use warnings;

my @hex = ("31","59", "4C","15","53","DD","54","31");
my $hex = join '', @hex;  # (1)
my $binary = unpack 'b*', pack 'H*', $hex;  # (2)

my @fields = $binary =~ /.{1,7}/g;
$_ = reverse foreach @fields;

print "$_\n" foreach @fields;

Nice approach.

(1) This assumes that values < "10" have a 0-prefix, so like "0A" and
not just "A".
Alternatives:
  my $hex = join '', map {sprintf "%02x", hex} @hex;
  my $hex = join '', map substr("00$_", -2), @hex;
  my $hex; $hex .= substr("00$_", -2) for @hex;

Or, since we don't really need the intermediate $hex, just

my $binary = unpack 'b*', join '', map chr hex, @hex;

But this is a beginners' group :)

Rob

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