John W. Krahn wrote:

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__

And of course you can also accomplish the same thing without converting the number to a string and back again:

#!/usr/bin/perl
use warnings;
use strict;

my $decimal_code = shift or die "usage: $0 <decimal code>\n";

my @itu_code;
{   my $dec = $decimal_code;
    for ( [ 0, 7 ], [ 3, 255 ], [ 8, 7 ] ) {
        my ( $shift, $mask ) = @$_;
        $dec >>= $shift;
        unshift @itu_code, $mask & $dec;
        }
    }

my @ansi_code;
{   my $dec = $decimal_code;
    for ( [ 0, 255 ], [ 8, 255 ], [ 8, 255 ] ) {
        my ( $shift, $mask ) = @$_;
        $dec >>= $shift;
        unshift @ansi_code, $mask & $dec;
        }
    }

printf "Decimal Code     = [%05d]\n", $decimal_code;
printf "ITU Cisco Code   = %03d-%03d-%03d\n", @itu_code;
printf "ANSI Cisco Codec = %03d-%03d-%03d\n", @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/


Reply via email to