Dharshana Eswaran wrote:
Hi all,
I wanted to know how to parse a 8bit binary data bitwise?
I have my input in hex and i convert it into binary form using the
following
logic:
%h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011",
4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111",
8 => "1000", 9 => "1001", a => "1010", b => "1011",
c => "1100", d => "1101", e => "1110", f => "1111",
);
$hex = "43";
($binary = $hex) =~ s/(.)/$h2b{lc $1}/g;
print $binary, "\n";
Now the output for the above prg is
01000011
Now i have to display this binary data as shown below:
0=> abc
100=> def
0011=> lmn
How to go about this?
I assume your 'abc', 'def' etc. are arbitrary data? The program below may help
you. Do come back here idf you have any questions.
Rob
use strict;
use warnings;
my $hex = "43";
my $binary = unpack 'B*', pack 'H*', $hex;
print "$binary\n\n";
my @fields = unpack 'A1A3A4', $binary;
my $i;
printf "%-4s => %s\n", $fields[$i++], $_ foreach qw/abc def lmn/;
**OUTPUT**
01000011
0 => abc
100 => def
0011 => lmn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>