### Re^2: How to increment a MAC Address?

by mda2 (Hermit)
 on Jun 16, 2005 at 17:04 UTC ( #467354=note: print w/replies, xml ) Need Help??

To increment a MAC Address, you need to convert HEX to DEC, Add AND convert DEC to HEX...

I propose this code.

```sub addmac {
my ( \$mac, \$add ) = @_;
\$mac =~ s/://g;
my \$dec = hex(\$mac);

return join(":", \$mac =~ /(..)/g );
}

my \$mac = '00:00:00:00:00:f1';
foreach ( 1 .. 32 ) {
print "\$mac\n";
}

I think others improve are possible, but it do a task.
```...
00:00:00:00:01:a5
00:00:00:00:01:cd
00:00:00:00:01:f9
00:00:00:00:02:29
00:00:00:00:02:5d
...
00:00:00:00:03:9d
00:00:00:00:03:e9
00:00:00:00:04:39
00:00:00:00:04:8d

--
Marco Antonio
Rio-PM

Replies are listed 'Best First'.
Re^3: How to increment a MAC Address?
by merlyn (Sage) on Jun 16, 2005 at 17:08 UTC
I did this:
```  # clear separators to obtain a hex string
\$mac =~ s/://g;
# convert hex string into a dec number
my \$dec = hex(\$mac);
# revert into a hex string

I don't see error here. I think it's used by context...

--
Marco Antonio
Rio-PM

```  # convert hex string into a dec number
my \$dec = hex(\$mac);
# revert into a hex string
My argument is with the word "dec" there. It's not decimal. It's just a number. In fact, it's more binary than it is decimal. {grin}

The "hex" operator interprets a hex string or an octal string, converting them to a number. A number is an abstraction, without a numeric base, until it gets converted to a string again.

Thus, you never really have a "decimal" number there. Just a number. Because you never printed it out.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Re^3: How to increment a MAC Address?
by ikegami (Pope) on Jun 17, 2005 at 23:13 UTC

Close, but not quite. You're trying to store a 48bit number into a 32bit variable. (Well, it's 32 bit on my Windows machine.) You have an overflow problem.

```my \$dec = hex('FF0000000000');
printf("%012x", \$dec);
__END__
Integer overflow in hexadecimal number at !.pl line 1
0000ffffffff

You could use Math::BigInt, or you could use floating point numbers (doubles) as I've previously detailed.

Create A New User
Node Status?
node history
Node Type: note [id://467354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?