Dharshana Eswaran wrote:

On 11/16/06, Rob Dixon <[EMAIL PROTECTED]> wrote:

Dharshana Eswaran wrote:

I am working on the code below:

my @hex = ("14", "2a", "11", "83");
$hex_length = @hex;

for($i=0; $i<$hex_length; $i++) {

my $binary = unpack 'B*', pack 'H*', $hex[$i];

print "$binary\n\n";

Oh dear. It looks like you have an ealier version of Perl where unpack has no
sub-templates, altthough I thought it had always been like that. Find out
what version you are running (with perl -v) and consider upgrading. I am
using v5.8.8 and bmost people use v5.8 or above.

In the meantime, replace

my @fields = unpack '(A7)*', $binary;

with

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


my @fields = $binary =~ /.{7,1}/g; This works fine

my @fields = $binary =~ /.{7,1}/g; Why does'nt this work?

Coz i wanted the procedure to be reversed, that is if the input is
"31","59", "4C","15","53"

I wanted the output to be

0110001
0110010
0110001
0101010
1110001
01010


The 1 and the 7 in the regex can't be exchanged as they are a minimum and
maximum number of characters to match. The pattern matches between one and seven
of any character, and so splits the 40-character string into five blocks of
seven characters and one of five.

I don't understand what you mean about reversing the procedure I'm afraid.

"31","59","4C","15","53"

is

00110001 01011001 01001100 00010101 01010011

in binary, and I can't see how to get to the bit patterns you give from that,
although it starts off close enough. Your fourth pattern of seven bits, for
instance, has three consecutive 1s in it, and that doesn't appear anywhere in
the number sequence you gave.

Are you sure the data you've posted is right?

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