MAC address conversion

I need to convert MAC addresses from canonical to non-canonical.  I know that there are lot of utilities like this out there already, ready to use.  But they only convert one mac address at a time.  I'd like to be able to convert one, or several, from a command line.  I've been able to convert a hex MAC address to binary, but that's where I get stuck.  After initial conversion to binary, I need to bitswap all of the binary digits, then convert back to hex.  Here's what I have so far:

$mac = $ARGV[0];

 sub hex2bin {
         for (my $i = 0; $i < length($mac); $i += 1) {
                 printf('%04b', hex(substr($mac, $i, 1)))
         print "\n";

"/usr/bin/perl 00f07f2671ef"  returns

Anyhelp would be appreciated!  Thanks - Rich
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


use Data::Dumper;

@ARGV = ("123456789ABC") unless defined @ARGV;
foreach my $mac (@ARGV) {
	print "mac ($mac) -> ", &hex2bin($mac), "\n";

 sub hex2bin {
 	my $hex_string = shift;
 	my @hex_array = split(//, $hex_string);
 	@hex_array = reverse(@hex_array);
 	my @result, @test;
 	my $string = '';
 	# convert to binary
 	foreach my $char (@hex_array) {
 		my $bin = reverse (sprintf "%04b", hex( $char ));
 		push @result, $bin;
 	print Dumper (\@result);
 	# now take binary rep and convert to hex
 	foreach my $char (@result) {
 		my $hex = sprintf('%X', oct("0b$char"));
 		$string .= $hex;
 	return join('', reverse(split(/(\w{2})/g, $string)));

 # 123456789ABC Canonical
 # 48-2C-6A-1E-59-3D non-canonical

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sol10Author Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.