comparing MAC addresses

S

smilesonisamal

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
 
V

Victor Bazarov

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
 
S

smilesonisamal

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

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

Victor Bazarov

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

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
 
S

smilesonisamal

(e-mail address 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
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

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).
 
V

Victor Bazarov

(e-mail address 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

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
 
S

smilesonisamal

(e-mail address removed) wrote:
(e-mail address 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
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

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

Victor Bazarov

(e-mail address removed) wrote:
(e-mail address 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

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
 
J

James Kanze

'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==.)
 
J

Juha Nieminen

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.
 
S

smilesonisamal

  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.

The input is from a binary file which contains series of octets.The
output is another file if the MAC address matches.
 
J

Juha Nieminen

The input is from a binary file which contains series of octets.The
output is another file if the MAC address matches.

In that case your question makes absolutely no sense. If the input is
binary, and the mac addresses are already in byte form, then there's no
conversion between "hexadecimal" and "decimal". Those are *ascii*
representations of numbers, not binary ones.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top