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

Dharshana Eswaran wrote:
>
> On 11/16/06, Rob Dixon <[EMAIL PROTECTED] > wrote:
>>
>> Dharshana Eswaran wrote:
>>>
>>> I am working on the code below:
>>>
[snip irrelevancies]
>>>
>>> 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";
>>>
>>> my @fields = unpack 'A2A2A4', $binary;
>>>
[snip irrelevancies]
>>>
>>> Now i need to split the binary value of the first element into two
halves
>>> (A7A1), seecond element into A6A2, third element into A5A3 and so
on...
>>>
>>> Then i need to display the 7bits of the first element, then join 1bit
of the
>>> first element to the 6bits of the second element(that is join A1 of
first
>>> element and A6of the second element) and then display it, then join A2
of
>>> the second element and A5 of the third element and then display it and
so
>>> on.
>>>
>>> How to go bout this?
>>
>> Does this help?
>>
>> Rob
>>
>>
>> use strict;
>> use warnings;
>>
>> my @hex = ("14", "2a", "11", "83");
>> my $hex = join '', @hex;
>> my $binary = unpack 'B*', pack 'H*', $hex;
>>
>> my @fields = unpack '(A7)*', $binary;
>> print $_, "\n" foreach @fields;
>>
>> **OUTPUT
>>
>> 0001010
>> 0001010
>> 1000010
>> 0011000
>> 0011
>
> my @fields = unpack '(A7)*', $binary;
>
> When i tried compiling, it says that
>
> "Invalid type in unpack: '(' at line 10."
>
> It does not accept this, i tried other combinations too. But it does not
> compile the program.
>
> What should i do?

(Please bottom-post your replies, as it makes long threads like this much
easier
to understand. Thanks.)

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;



Rob





Rob,

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


Thanks and Regards,
Dharshana

Reply via email to