我想将mac地址更改为浮点数。
我尝试在线使用IEEE 754转换器,但它说"值超出了范围"。
我怎样才能以正确的方式做到这一点?
答案 0 :(得分:1)
# ORIGINAL VIA PERLMONKS
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";
测试:
$ perl mac2num 00:00:DE:AD:BE:EF
00:00:DE:AD:BE:EF converted to a decimal: 3735928559
3735928559 converted to hex: 00:00:de:ad:be:ef