Discussion:
Comparing Mac address
(too old to reply)
s***@gmail.com
2009-04-09 22:47:25 UTC
Permalink
Hi all,
I was just looking for a example code in C++ which converts 2 mac
address
(both are in decimal form). Can anybody help me in this regard?

I mean 2 mac address 00:5E:0A:A0:AA-> 00:94:xx:xx:xx:xx(decimal form)

Regards
Soni

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Ulrich Eckhardt
2009-04-10 08:18:18 UTC
Permalink
Post by s***@gmail.com
I was just looking for a example code in C++ which converts 2 mac
address (both are in decimal form). Can anybody help me in this regard?
I mean 2 mac address 00:5E:0A:A0:AA-> 00:94:xx:xx:xx:xx(decimal form)
Hmm, not clear what you want...

- The subject talks about comparing while your text talks about converting.
- You claim the MAC addresses are in "decimal form" but the strings you show
use hexadecimal for the contained octets.
- What are the "xx:xx:xx"? They surely are not part of any type of MAC
address representation I know.

Anyway: if you really want to compare the addresses as per your subject,
there are two ways:
1. If you really have them in a textual representation (strings), you could
use a case-insensitive comparison (or apply toupper() on all elements) and
then use the built-in operator< for std::string.

2. If you actually have sequences of six octets, you can use memcmp() to
compare them.

Uli


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jim Langston
2009-04-10 08:38:41 UTC
Permalink
Post by s***@gmail.com
Hi all,
I was just looking for a example code in C++ which converts 2 mac
address
(both are in decimal form). Can anybody help me in this regard?
I mean 2 mac address 00:5E:0A:A0:AA-> 00:94:xx:xx:xx:xx(decimal form)
Regards
Soni
Rather trivial to convert bases. I did a quick google for "c++ convert 16
10" and got some hits.
It's simple, 0 to 9 are 0 to 9. A is 10, F is 15. If it's in the 16'ths
place (would be 10's place in decimal) multiply it by 16.
Lets take your sample:

00 is 00
5E. 5*16+14 = 94.
You try 0A. If you get 10 you're right.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
s***@gmail.com
2009-04-10 20:16:52 UTC
Permalink
Post by Jim Langston
Post by s***@gmail.com
Hi all,
I was just looking for a example code in C++ which converts 2 mac
address
(both are in decimal form). Can anybody help me in this regard?
I mean 2 mac address 00:5E:0A:A0:AA-> 00:94:xx:xx:xx:xx(decimal form)
Regards
Soni
Rather trivial to convert bases. I did a quick google for "c++ convert 16
10" and got some hits.
It's simple, 0 to 9 are 0 to 9. A is 10, F is 15. If it's in the 16'ths
place (would be 10's place in decimal) multiply it by 16.
00 is 00
5E. 5*16+14 = 94.
You try 0A. If you get 10 you're right.
Sorry for the confusion. What I want is I have 2 MAC address
1. 00:5E:6E:2A:AA:BB
2. AA:BB:CC:DD:EE:FF

The first MAC address is in decimal format. 00,94,110,42,xx,xx
The second MAC address is in MAC address Format AA:BB:CC:DD:EE:FF

The second mac address needs to be converted to decimal and compare
both the mac address for equality.Can anybody help me in this regard?
A sample source code will be helpful for me.

Regards
Soni



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
paralysis
2009-04-16 22:48:14 UTC
Permalink
Post by s***@gmail.com
Sorry for the confusion. What I want is I have 2 MAC address
1. 00:5E:6E:2A:AA:BB
2. AA:BB:CC:DD:EE:FF
The first MAC address is in decimal format. 00,94,110,42,xx,xx
The second MAC address is in MAC address Format AA:BB:CC:DD:EE:FF
I don't know a lot about the different representations of MACs, but I
would compare each of the six "sections" of the MAC address one by
one. To do this, convert the text representations of each section to a
common ground, like an int.

You'll need to write a routine converting a decimal string to an int,
and a hexadecimal string to an int. Then compare the ints.

A decimal-to-int routine would consist basically of

int atoi(char const * s) {
unsigned i = 0;
int value = 0;
while (s && '0' < s[i] && s[i] < '9')
value = value * 10 + (s[i++] - '0');
return value;
}

A hexadecimal routine, htoi, is a little more complicated because you
have to handle the ASCII letters 'a' through 'f' and 'A' through 'F'.
I'd also worry about "i" running away past the end of "s" in a robust
implementation.

So, that would basically give us

bool are_macs_equal(char const * dmac, char const *hmac) {
int j = 0;
unsigned idmac = 0;
unsigned ihmac = 0;

while (j < 6) {
if (atoi(dmac + idmac) != htoi(hmac + ihmac))
break; // they're different, bail
j++; // another section is equal
while (dmac[idmac++] != ':') /* scan past delimiter */;
while (hmac[ihmac++] != ':') /* ditto */;
}
return (j == 6);
}

Actually, you'll want to watch that "while(dmac[idmac++] != ':')"
doesn't skip over the terminating null character either. But that's
the basic idea.
--Jonathan

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Loading...