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; (2) A value like "XX" in @hex is silently accepted by pack(). <quote src="perldoc -f pack"> The "h" and "H" fields pack a string that many nybbles (4-bit groups, representable as hexadecimal digits, 0-9a-f) long. Each byte of the input field of pack() generates 4 bits of the result. For non-alphabetical bytes the result is based on the 4 least-significant bits of the input byte, i.e., on "ord($byte)%16". In particular, bytes "0" and "1" generate nybbles 0 and 1, as do bytes "\0" and "\1". For bytes "a".."f" and "A".."F" the result is compatible with the usual hexadecimal digits, so that "a" and "A" both generate the nybble "0xa==10". The result for bytes "g".."z" and "G".."Z" is not well-defined. </quote> -- 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>