Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > comparing MAC addresses

Reply
Thread Tools

comparing MAC addresses

 
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
Hi all,
I was just looking for a example code in C++ which converts 2 mac
address.


Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
Mac Address B: 1 0 94 0 0 2

I want to convert Mac address A to decimal form and then compare both
MAC address are equal or not. Any example code will be helpful for me.

Could anybody help me in this regard?

Thanks in advance
Regards
Soni
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
> I was just looking for a example code in C++ which converts 2 mac
> address.
>
>
> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
> Mac Address B: 1 0 94 0 0 2
>
> I want to convert Mac address A to decimal form and then compare both
> MAC address are equal or not. Any example code will be helpful for me.
>
> Could anybody help me in this regard?


Parsing a string is done using sscanf or isstream, you can simply read
the values between the colons into elements of an array. Then you
compare the elements of one array with the other in a loop (or you could
roll it out, if there only a few elements and the size is fixed).

Reading from a string (example):

std::string MacAddressA("00:0E:AA:BB:CC:EA");
int valuesA[6];
char colon;
std::istringstream is(MacAddressA);
is >> std::hex >> valuesA[0] >> colon
>> valuesA[1] >> colon
>> valuesA[2] >> colon
>> valuesA[3] >> colon
>> valuesA[4] >> colon
>> valuesA[5];


Another example

std::string MacAddressA("00:0E:AA:BB:CC:EA");
int valuesA[6];
sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
valuesA, valuesA+1, valuesA+2,
valuesA+3, valuesA+4, valuesA+5);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 7:10*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi all,
> > * * I was just looking for a example code in C++ which converts 2 mac
> > address.

>
> > Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
> > Mac Address B: *1 0 94 0 0 2

>
> > I want to convert Mac address A to decimal form and then compare both
> > MAC address are equal or not. Any example code will be helpful for me.

>
> > Could anybody help me in this regard?

>
> Parsing a string is done using sscanf or isstream, you can simply read
> the values between the colons into elements of an array. *Then you
> compare the elements of one array with the other in a loop (or you could
> roll it out, if there only a few elements and the size is fixed).
>
> Reading from a string (example):
>
> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> * * *int valuesA[6];
> * * *char colon;
> * * *std::istringstream is(MacAddressA);
> * * *is >> std::hex >> valuesA[0] >> colon
> * * * * * * * * * * >> valuesA[1] >> colon
> * * * * * * * * * * >> valuesA[2] >> colon
> * * * * * * * * * * >> valuesA[3] >> colon
> * * * * * * * * * * >> valuesA[4] >> colon
> * * * * * * * * * * >> valuesA[5];
>
> Another example
>
> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> * * *int valuesA[6];
> * * *sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
> * * * * * * *valuesA, valuesA+1, valuesA+2,
> * * * * * * * * valuesA+3, valuesA+4, valuesA+5);
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Thanks for help..In that case I need to convert the hexadecimal to
decimal again.Is there a way to convert it to decimal from mac address?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
(E-Mail Removed) wrote:
> On Apr 10, 7:10 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Hi all,
>>> I was just looking for a example code in C++ which converts 2 mac
>>> address.
>>> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
>>> Mac Address B: 1 0 94 0 0 2
>>> I want to convert Mac address A to decimal form and then compare both
>>> MAC address are equal or not. Any example code will be helpful for me.
>>> Could anybody help me in this regard?

>> Parsing a string is done using sscanf or isstream, you can simply read
>> the values between the colons into elements of an array. Then you
>> compare the elements of one array with the other in a loop (or you could
>> roll it out, if there only a few elements and the size is fixed).
>>
>> Reading from a string (example):
>>
>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>> int valuesA[6];
>> char colon;
>> std::istringstream is(MacAddressA);
>> is >> std::hex >> valuesA[0] >> colon
>> >> valuesA[1] >> colon
>> >> valuesA[2] >> colon
>> >> valuesA[3] >> colon
>> >> valuesA[4] >> colon
>> >> valuesA[5];

>>
>> Another example
>>
>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>> int valuesA[6];
>> sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
>> valuesA, valuesA+1, valuesA+2,
>> valuesA+3, valuesA+4, valuesA+5);
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> Thanks for help..In that case I need to convert the hexadecimal to
> decimal again.Is there a way to convert it to decimal from mac address?


I am sorry, I don't understand the question.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 7:32*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Apr 10, 7:10 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >>> Hi all,
> >>> * * I was just looking for a example code in C++ which converts 2 mac
> >>> address.
> >>> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
> >>> Mac Address B: *1 0 94 0 0 2
> >>> I want to convert Mac address A to decimal form and then compare both
> >>> MAC address are equal or not. Any example code will be helpful for me..
> >>> Could anybody help me in this regard?
> >> Parsing a string is done using sscanf or isstream, you can simply read
> >> the values between the colons into elements of an array. *Then you
> >> compare the elements of one array with the other in a loop (or you could
> >> roll it out, if there only a few elements and the size is fixed).

>
> >> Reading from a string (example):

>
> >> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> >> * * *int valuesA[6];
> >> * * *char colon;
> >> * * *std::istringstream is(MacAddressA);
> >> * * *is >> std::hex >> valuesA[0] >> colon
> >> * * * * * * * * * * >> valuesA[1] >> colon
> >> * * * * * * * * * * >> valuesA[2] >> colon
> >> * * * * * * * * * * >> valuesA[3] >> colon
> >> * * * * * * * * * * >> valuesA[4] >> colon
> >> * * * * * * * * * * >> valuesA[5];

>
> >> Another example

>
> >> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> >> * * *int valuesA[6];
> >> * * *sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
> >> * * * * * * *valuesA, valuesA+1, valuesA+2,
> >> * * * * * * * * valuesA+3, valuesA+4, valuesA+5);

>
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

>
> > Thanks for help..In that case I need to convert the hexadecimal to
> > decimal again.Is there a way to convert it to decimal from mac address?

>
> I am sorry, I don't understand the question.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


What I mean is if the value[0],value[1] ....value[6] will be parsed
individually then I need to convert the hex values of each values to
convert to decimal again to compare with the other decimal formatted
string(1 0 94 0 0 2).
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
(E-Mail Removed) wrote:
> On Apr 10, 7:32 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> On Apr 10, 7:10 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>>>> (E-Mail Removed) wrote:
>>>>> Hi all,
>>>>> I was just looking for a example code in C++ which converts 2 mac
>>>>> address.
>>>>> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
>>>>> Mac Address B: 1 0 94 0 0 2
>>>>> I want to convert Mac address A to decimal form and then compare both
>>>>> MAC address are equal or not. Any example code will be helpful for me.
>>>>> Could anybody help me in this regard?
>>>> Parsing a string is done using sscanf or isstream, you can simply read
>>>> the values between the colons into elements of an array. Then you
>>>> compare the elements of one array with the other in a loop (or you could
>>>> roll it out, if there only a few elements and the size is fixed).
>>>> Reading from a string (example):
>>>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>>>> int valuesA[6];
>>>> char colon;
>>>> std::istringstream is(MacAddressA);
>>>> is >> std::hex >> valuesA[0] >> colon
>>>> >> valuesA[1] >> colon
>>>> >> valuesA[2] >> colon
>>>> >> valuesA[3] >> colon
>>>> >> valuesA[4] >> colon
>>>> >> valuesA[5];
>>>> Another example
>>>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>>>> int valuesA[6];
>>>> sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
>>>> valuesA, valuesA+1, valuesA+2,
>>>> valuesA+3, valuesA+4, valuesA+5);
>>>> V
>>>> --
>>>> Please remove capital 'A's when replying by e-mail
>>>> I do not respond to top-posted replies, please don't ask
>>> Thanks for help..In that case I need to convert the hexadecimal to
>>> decimal again.Is there a way to convert it to decimal from mac address?

>> I am sorry, I don't understand the question.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> What I mean is if the value[0],value[1] ....value[6] will be parsed
> individually then I need to convert the hex values of each values to
> convert to decimal again to compare with the other decimal formatted
> string(1 0 94 0 0 2).


What if you just read the decimal-formatted string into another array
and compare the internal representations instead? Just a thought...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 7:58*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Apr 10, 7:32 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >>> On Apr 10, 7:10 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >>>> (E-Mail Removed) wrote:
> >>>>> Hi all,
> >>>>> * * I was just looking for a example code in C++ which converts 2 mac
> >>>>> address.
> >>>>> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
> >>>>> Mac Address B: *1 0 94 0 0 2
> >>>>> I want to convert Mac address A to decimal form and then compare both
> >>>>> MAC address are equal or not. Any example code will be helpful for me.
> >>>>> Could anybody help me in this regard?
> >>>> Parsing a string is done using sscanf or isstream, you can simply read
> >>>> the values between the colons into elements of an array. *Then you
> >>>> compare the elements of one array with the other in a loop (or you could
> >>>> roll it out, if there only a few elements and the size is fixed).
> >>>> Reading from a string (example):
> >>>> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> >>>> * * *int valuesA[6];
> >>>> * * *char colon;
> >>>> * * *std::istringstream is(MacAddressA);
> >>>> * * *is >> std::hex >> valuesA[0] >> colon
> >>>> * * * * * * * * * * >> valuesA[1] >> colon
> >>>> * * * * * * * * * * >> valuesA[2] >> colon
> >>>> * * * * * * * * * * >> valuesA[3] >> colon
> >>>> * * * * * * * * * * >> valuesA[4] >> colon
> >>>> * * * * * * * * * * >> valuesA[5];
> >>>> Another example
> >>>> * * *std::string MacAddressA("00:0E:AA:BB:CC:EA");
> >>>> * * *int valuesA[6];
> >>>> * * *sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
> >>>> * * * * * * *valuesA, valuesA+1, valuesA+2,
> >>>> * * * * * * * * valuesA+3, valuesA+4, valuesA+5);
> >>>> V
> >>>> --
> >>>> Please remove capital 'A's when replying by e-mail
> >>>> I do not respond to top-posted replies, please don't ask
> >>> Thanks for help..In that case I need to convert the hexadecimal to
> >>> decimal again.Is there a way to convert it to decimal from mac address?
> >> I am sorry, I don't understand the question.

>
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

>
> > What I mean is if the value[0],value[1] ....value[6] will be parsed
> > individually then I need to convert the hex values of each values to
> > convert to decimal again to compare with the other decimal formatted
> > string(1 0 94 0 0 2).

>
> What if you just read the decimal-formatted string into another array
> and compare the internal representations instead? *Just a thought...
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


I was thinking to use memcmp.do u have any idea?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
(E-Mail Removed) wrote:
> On Apr 10, 7:58 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> On Apr 10, 7:32 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>>>> (E-Mail Removed) wrote:
>>>>> On Apr 10, 7:10 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>>>>>> (E-Mail Removed) wrote:
>>>>>>> Hi all,
>>>>>>> I was just looking for a example code in C++ which converts 2 mac
>>>>>>> address.
>>>>>>> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
>>>>>>> Mac Address B: 1 0 94 0 0 2
>>>>>>> I want to convert Mac address A to decimal form and then compare both
>>>>>>> MAC address are equal or not. Any example code will be helpful for me.
>>>>>>> Could anybody help me in this regard?
>>>>>> Parsing a string is done using sscanf or isstream, you can simply read
>>>>>> the values between the colons into elements of an array. Then you
>>>>>> compare the elements of one array with the other in a loop (or you could
>>>>>> roll it out, if there only a few elements and the size is fixed).
>>>>>> Reading from a string (example):
>>>>>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>>>>>> int valuesA[6];
>>>>>> char colon;
>>>>>> std::istringstream is(MacAddressA);
>>>>>> is >> std::hex >> valuesA[0] >> colon
>>>>>> >> valuesA[1] >> colon
>>>>>> >> valuesA[2] >> colon
>>>>>> >> valuesA[3] >> colon
>>>>>> >> valuesA[4] >> colon
>>>>>> >> valuesA[5];
>>>>>> Another example
>>>>>> std::string MacAddressA("00:0E:AA:BB:CC:EA");
>>>>>> int valuesA[6];
>>>>>> sscanf(MacAddressA.c_str(), "%x:%x:%x:%x:%x:%x",
>>>>>> valuesA, valuesA+1, valuesA+2,
>>>>>> valuesA+3, valuesA+4, valuesA+5);
>>>>>> V
>>>>>> --
>>>>>> Please remove capital 'A's when replying by e-mail
>>>>>> I do not respond to top-posted replies, please don't ask
>>>>> Thanks for help..In that case I need to convert the hexadecimal to
>>>>> decimal again.Is there a way to convert it to decimal from mac address?
>>>> I am sorry, I don't understand the question.
>>>> V
>>>> --
>>>> Please remove capital 'A's when replying by e-mail
>>>> I do not respond to top-posted replies, please don't ask
>>> What I mean is if the value[0],value[1] ....value[6] will be parsed
>>> individually then I need to convert the hex values of each values to
>>> convert to decimal again to compare with the other decimal formatted
>>> string(1 0 94 0 0 2).

>> What if you just read the decimal-formatted string into another array
>> and compare the internal representations instead? Just a thought...
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> I was thinking to use memcmp.do u have any idea?


'memcpy' might work. How about

if (array[0] == vec[0]
&& array[1] == vec[1]
&& array[2] == vec[2]
&& array[3] == vec[3]
&& array[4] == vec[4]
&& array[5] == vec[5])
...
?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 7:43 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I was thinking to use memcmp.do u have any idea?


> 'memcpy' might work. How about


> if (array[0] == vec[0]
> && array[1] == vec[1]
> && array[2] == vec[2]
> && array[3] == vec[3]
> && array[4] == vec[4]
> && array[5] == vec[5])
> ...
> ?


What about std::equal? That's what it's there for. For
that matter, if he reads them into std::vector (I presume
that's what you mean by array), he can just compare the
vectors: if ( array == vec ). Or if that were all that were
in the strings, he could use std::replace to replace the ':'
with ' ', then use two std::istream_iterator<int> and
std::lexicographical_compare. (Seriously, the cleanest
solution is probably to define a class MacAddress, which
supported comparison, and had an operator>>. But that's a
fair amount of work if he's only going to do this once. And
of course, the class itself would use the techniques I just
spoke of, probably keeping the actual value in an
std::vector, and using == on those in its operator==.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-11-2009
(E-Mail Removed) wrote:
> Hi all,
> I was just looking for a example code in C++ which converts 2 mac
> address.
>
>
> Mac Address A : 00:0E:AA:BB:CC:EA(Hexadecimal form)
> Mac Address B: 1 0 94 0 0 2
>
> I want to convert Mac address A to decimal form and then compare both
> MAC address are equal or not. Any example code will be helpful for me.
>
> Could anybody help me in this regard?


There's a major problem in your question: You haven't specified where
you are reading those addresses from and in which format.

Are those addresses in an ascii file which you are trying to read, or
something like that? Can you give us an example of such file so that we
would know better exactly what is it that you are trying to achieve. And
where do you want that converted address to be output? To another ascii
file?

It's basically impossible to answer your question "how to convert from
Mac Address A to Mac Address B" without knowing how you are reading the
first address in the first place, or where the converted address should
be output.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses cagdas.gerede@gmail.com C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
NEW TOOL: SMAC-CL 1.0, Console App for modifying MAC addresses kl Wireless Networking 2 07-19-2004 07:12 AM
ADSL modem registers 2 mac addresses? Han Valk Cisco 3 01-27-2004 11:47 AM
mac-addresses,speed of ports of 2948G switch..Need help..Urgent Arunkumar Cisco 2 01-14-2004 05:40 PM



Advertisments