Velocity Reviews > Perl > decrease MAC

# decrease MAC

jammer
Guest
Posts: n/a

 03-17-2008
I have a hex number which is actually a MAC address and I want to find
the MAC immediately before.
0000AAAA9999

Is there a way to treat it as a hex number and do -1 one on it?

Ben Morrow
Guest
Posts: n/a

 03-17-2008

Quoth jammer <(E-Mail Removed)>:
> I have a hex number which is actually a MAC address and I want to find
> the MAC immediately before.
> 0000AAAA9999
>
> Is there a way to treat it as a hex number and do -1 one on it?

my \$MAC = '0000AAAA9999';
printf "%x\n", hex(\$MAC) - 1;

Ben

jammer
Guest
Posts: n/a

 03-17-2008
On Mar 17, 4:06 pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth jammer <(E-Mail Removed)>:
>
> > I have a hex number which is actually a MAC address and I want to find
> > the MAC immediately before.
> > 0000AAAA9999

>
> > Is there a way to treat it as a hex number and do -1 one on it?

>
> my \$MAC = '0000AAAA9999';
> printf "%x\n", hex(\$MAC) - 1;
>
> Ben

What if there are not 4 leading 0s.
my \$MAC = '000BAAAA9999';
printf "%x\n", hex(\$MAC) - 1;

Jürgen Exner
Guest
Posts: n/a

 03-17-2008
jammer <(E-Mail Removed)> wrote:
>I have a hex number which is actually a MAC address and I want to find
>the MAC immediately before.
>0000AAAA9999
>
>Is there a way to treat it as a hex number and do -1 one on it?

"God made the natural numbers, all else is men's work"
There is no such thing as a hex(adezimal) number. You got numbers and that
is what Perl knows about and where you can add and subtract and do other
fancy stuff with.

And then you can input as well as print those numbers in different
representations, e.g. as a hexadecimal representation of a number.
- to convert the hex representation of a number into a number use hex()
- to print the hexadezimal representation of a number use printf

jue

Ben Morrow
Guest
Posts: n/a

 03-17-2008

Quoth Tad J McClellan <(E-Mail Removed)>:
> jammer <(E-Mail Removed)> wrote:
>
> > my \$MAC = '000BAAAA9999';

>
> What _is_ significant is the number of bits required to represent the number.
>
> ie. 32 bits required for Ben's number, but 36 bits are required
>
> > printf "%x\n", hex(\$MAC) - 1;
> > Integer overflow in hexadecimal number

>
> What do you know?
>
> The message is exactly right, assuming you are on a 32-bit processor.

And there are two solutions: use a perl built for 64-bit integers
(practically every processor nowadays has a 64-bit integral type, and
perl can usually use these if asked to); or use 'bigint', which will
allow arbitrary-sized integers at the expense of some speed.

Ben

Jürgen Exner
Guest
Posts: n/a

 03-17-2008
jammer <(E-Mail Removed)> wrote:
>On Mar 17, 4:06 pm, Ben Morrow <(E-Mail Removed)> wrote:
>> Quoth jammer <(E-Mail Removed)>:
>> > 0000AAAA9999
>> > Is there a way to treat it as a hex number and do -1 one on it?

>>
>> my \$MAC = '0000AAAA9999';
>> printf "%x\n", hex(\$MAC) - 1;

>
>What if there are not 4 leading 0s.

So what? Doesn't matter.

>my \$MAC = '000BAAAA9999';
>printf "%x\n", hex(\$MAC) - 1;

That error message simply indicates that the numerical value exceeds the
largest possible number in this version of the perl interpreter.
What is the output of 'perl -V' on your system?

Maybe the bigint module will help on systems with a low maxint, but that is
just a guess.

jue

Josef Moellers
Guest
Posts: n/a

 03-18-2008
jammer wrote:
> On Mar 17, 4:06 pm, Ben Morrow <(E-Mail Removed)> wrote:
>> Quoth jammer <(E-Mail Removed)>:
>>
>>> I have a hex number which is actually a MAC address and I want to find
>>> the MAC immediately before.
>>> 0000AAAA9999
>>> Is there a way to treat it as a hex number and do -1 one on it?

>> my \$MAC = '0000AAAA9999';
>> printf "%x\n", hex(\$MAC) - 1;
>>
>> Ben

>
> What if there are not 4 leading 0s.
> my \$MAC = '000BAAAA9999';
> printf "%x\n", hex(\$MAC) - 1;
> Integer overflow in hexadecimal number

Pragmatic approach: split up into smaller parts, do the math, then
re-assemble.

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

jammer
Guest
Posts: n/a

 03-18-2008
On Mar 18, 5:29 am, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> jammer wrote:
> > On Mar 17, 4:06 pm, Ben Morrow <(E-Mail Removed)> wrote:
> >> Quoth jammer <(E-Mail Removed)>:

>
> >>> I have a hex number which is actually a MAC address and I want to find
> >>> the MAC immediately before.
> >>> 0000AAAA9999
> >>> Is there a way to treat it as a hex number and do -1 one on it?
> >> my \$MAC = '0000AAAA9999';
> >> printf "%x\n", hex(\$MAC) - 1;

>
> >> Ben

>
> > What if there are not 4 leading 0s.
> > my \$MAC = '000BAAAA9999';
> > printf "%x\n", hex(\$MAC) - 1;
> > Integer overflow in hexadecimal number

>
> Pragmatic approach: split up into smaller parts, do the math, then
> re-assemble.
>

Good idea.