Rajeev Prasad wrote:
Hello,
Hello,
I am trying to convert 32 bitx Hex point code to 24 bit (8-8-8) point
code. i tried to use below script from net, but due to limited
understanding of perl and above script, I was not able to resolve my
problem.
Can someone please help me resolve my issue?
thank you.
Rajeev
#!/usr/bin/perl -w
my $decimalcode = shift;
my $binarycode = 0b0;
my @ciscocode ;
$binarycode = unpack(”B32″,pack(”N”,$decimalcode));
my @itucode = grep {$_ ne ”}
split(/(\d{18})(\d{3})(\d{8})(\d{3})/,$binarycode);
my @ansicode = grep {$_ ne ”}
split(/(\d{8})(\d{8})(\d{8})(\d{8})/,$binarycode);
print “itucode = $itucode[1] $itucode[2] $itucode[3]\n”;
print “ansicode = $ansicode[1] $ansicode[2] $ansicode[3]\n”;
my $bintest = unpack(”N”,pack(”B32″,substr(”0″ x 32 . $binarycode, -32)));
printf (”Decimal Code = [%05d]\n”,$bintest);
@ciscocode = (unpack(”N”,pack(”B32″,substr(”0″ x 32 . $itucode[1], -32))),
unpack(”N”,pack(”B32″,substr(”0″ x 32 . $itucode[2], -32))),
unpack(”N”,pack(”B32″,substr(”0″ x 32 . $itucode[3], -32))));
printf (”ITU Cisco Code = %03d-%03d-%03d\n”,
$ciscocode[0],$ciscocode[1],$ciscocode[2]);
@ciscocode = (unpack(”N”,pack(”B32″,substr(”0″ x 32 . $ansicode[1], -32))),
unpack(”N”,pack(”B32″,substr(”0″ x 32 . $ansicode[2], -32))),
unpack(”N”,pack(”B32″,substr(”0″ x 32 . $ansicode[3], -32))));
printf (”ANSI Cisco Codec = %03d-%03d-%03d\n”,
$ciscocode[0],$ciscocode[1],$ciscocode[2]);
You say you found this code "on the net"? It would probably be better as:
$!/usr/bin/perl
use warnings;
use strict;
my $decimal_code = shift or die "usage: $0 <decimal code>\n";
my $binary_code = unpack 'B32', pack 'N', $decimal_code;
my @itu_code = $binary_code =~ / (\d{3}) (\d{8}) (\d{3}) $ /x;
my @ansi_code = $binary_code =~ / (\d{8}) (\d{8}) (\d{8}) $ /x;
print "itucode = @itu_code\n”;
print "ansicode = @ansi_code\n”;
printf "Decimal Code = [%05d]\n", $decimal_code;
printf "ITU Cisco Code = %03d-%03d-%03d\n", map oct( '0b' . $_ ),
@itu_code;
printf "ANSI Cisco Codec = %03d-%03d-%03d\n", map oct( '0b' . $_ ),
@ansi_code;
__END__
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/