decrease MAC

J

jammer

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?
 
B

Ben Morrow

Quoth jammer said:
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
 
J

jammer

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
 
J

Jürgen Exner

jammer said:
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
 
B

Ben Morrow

Quoth Tad J McClellan said:
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
for your 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

Jürgen Exner

jammer said:
What if there are not 4 leading 0s.

So what? Doesn't matter.
my $MAC = '000BAAAA9999';
printf "%x\n", hex($MAC) - 1;
Integer overflow in hexadecimal number

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
 
J

Josef Moellers

jammer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top