justintime has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a need to store a MAC address as an integer in a database. Since a MAC is a 48bit, 3 octet hex, it's not too different from the 32bit, 4 octet decimal of an IP address. There is a lot of code out there for converting IP->Long, but none for MAC->Long. I've darn near got it, but I'm stumped now. Here's what I have:
sub mac2long { my @mac = unpack("A4"x3,shift); my $long = 0; foreach my $octet (@mac) { $long <<= 16; $long |= hex($octet); } $long; } sub long2mac { my $long = shift; my @octets; for (my $i = 2; $i >= 0; $i--) { $octets[$i] = sprintf("%04x",($long & 0xFFFF)); $long >>= 16; } join('.',@octets); } chomp(my $inmac = shift @ARGV); my $long = mac2long($inmac); print "$inmac converted to long: $long\n"; my $outmac = long2mac($long); print "$long converted to mac: $outmac\n";
'Executing mac2int.pl 001095123456' results in:
001095123456 converted to long: 2500998230 2500998230 converted to mac: 0000.9512.3456
For the life of me, I can't figure out why I can't get the last remaining octet back. Anyone have any tips? Justin

Replies are listed 'Best First'.
Re: Help with Mac Address to Integer conversion
by ikegami (Patriarch) on Mar 18, 2005 at 18:33 UTC

    You're trying to store a 48bit number into a 32bit variable. mac2long is returning 2500998230 (0x95123456), while it should be returning 71220474966 (0x1095123456). You could use Math::BigInt.

    By the way, "octet" means "8 bits", so the MAC addr is 6 (48/8) octets long, not 3.

      <<, | and/or & are forcing an integer context. The float or double that my Perl (Win32) uses can hold a 48 bit integer with no loss of precision, so you could rewrite your code to avoid using operators that force Perl to work with integers. What follows is such code:

      sub mac_hex2num { my $mac_hex = shift; $mac_hex =~ s/://g; $mac_hex = substr(('0'x12).$mac_hex, -12); my @mac_bytes = unpack("A2"x6, $mac_hex); my $mac_num = 0; foreach (@mac_bytes) { $mac_num = $mac_num * (2**8) + hex($_); } return $mac_num; } sub mac_num2hex { my $mac_num = shift; my @mac_bytes; for (1..6) { unshift(@mac_bytes, sprintf("%02x", $mac_num % (2**8))); $mac_num = int($mac_num / (2**8)); } return join(':', @mac_bytes); } chomp(my $inmac = shift @ARGV); my $mac = mac_hex2num($inmac); print "$inmac converted to a decimal: $mac\n"; my $outmac = mac_num2hex($mac); print "$mac converted to hex: $outmac\n"; __END__ >perl mac2num.pl FEDCBA98765F FEDCBA98765F converted to a decimal: 280223976814175 280223976814175 converted to hex: fe:dc:ba:98:76:5f >perl mac2num.pl 2500998230 2500998230 converted to a decimal: 158923850288 158923850288 converted to hex: 00:25:00:99:82:30 >perl mac2num.pl FFFFFFFFFFFF FFFFFFFFFFFF converted to a decimal: 281474976710655 281474976710655 converted to hex: ff:ff:ff:ff:ff:ff

      By the way, you had a bug when fewer than 12 digits were provided. "2500998230" was being treated as "0x250099820030" instead of "0x002500998230". The substr business in mac_hex2num fixes the problem.

Re: Help with Mac Address to Integer conversion
by DrHyde (Prior) on Mar 21, 2005 at 09:39 UTC
    The << operator overflows if it would go above 32 bits on a 32 bit machine, and the result is undefined. So unless you know that you won't overflow (and for dealing with 48 bit MACs this means you need to be on a 64 bit machine with a 64 bit perl) then you need to replace all your $i << $j with $i *= 2 ** $j.

    I consider this to be a bug in perl. The other mathemagical operators DTRT when you would overflow an int and auto-promote your data from an int to a double. This should do the same. Yes, there's a corresponding loss of precision, but if we accept that for multiplication we should accept it for shifting.

A reply falls below the community's threshold of quality. You may see it by logging in.