Converts a Token-Ring MAC-Address into an Ethernet MAC-Address and vise versa. Demonstrates the usage of hashes.
Author: G. Lange, glange@libero.it
#!usr/bin/perl -w # Converts a Token-Ring MAC-Address into # an Ethernet and vise versa. # Hashes # MAC-Address Format: 00-30-F5-07-5D-7D $0 =~ s|^.*[\\/]||; $usage="Usage: $0 <mac-address>\nFormat of the mac-address: xx-xx-xx-x +x-xx-xx\n"; if ($ARGV[0] eq "") { print $usage; exit; } %convert = ( "0" => "0", "1" => "8", "2" => "4", "3" => "C", "4" => "2", "5" => "A", "6" => "6", "7" => "E", "8" => "1", "9" => "9", "A" => "5", "B" => "D", "C" => "3", "D" => "B", "E" => "7", "F" => "F", ); $mac = $ARGV[0]; @mac_array = split( /\-/ , $mac); foreach $a (0 .. $#mac_array) { $c_mac[$a] = $convert{substr(uc($mac_array[$a]),1,1)} . $convert{substr(uc($mac_array[$a]),0,1)}; # if ($a < ($#mac_array)) { $c_mac[$a].= "-"; } } $mac = join("-", @c_mac); print $mac;

Replies are listed 'Best First'.
Re: Token-Ring MAC-Address
by rob_au (Abbot) on May 18, 2004 at 00:14 UTC
    A one-line version ...
    perl -le 'print join "-", map { y/0123456789ABCDEFabcdef/084C2A6E195D3 +B7F5D3B7F/; $_ = reverse } split /-/, $ARGV[0]' xx-xx-xx-xx-xx-xx

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011011010'"