comparing MAC addresses

Discussion in 'C++' started by smilesonisamal@gmail.com, Apr 10, 2009.

  1. Guest

    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
     
    , Apr 10, 2009
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 10, 2009
    #2
    1. Advertising

  3. Guest

    On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    > 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?
     
    , Apr 10, 2009
    #3
  4. wrote:
    > On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    >> 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
     
    Victor Bazarov, Apr 10, 2009
    #4
  5. Guest

    On Apr 10, 7:32 pm, Victor Bazarov <> wrote:
    > wrote:
    > > On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    > >> 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).
     
    , Apr 10, 2009
    #5
  6. wrote:
    > On Apr 10, 7:32 pm, Victor Bazarov <> wrote:
    >> wrote:
    >>> On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    >>>> 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
     
    Victor Bazarov, Apr 10, 2009
    #6
  7. Guest

    On Apr 10, 7:58 pm, Victor Bazarov <> wrote:
    > wrote:
    > > On Apr 10, 7:32 pm, Victor Bazarov <> wrote:
    > >> wrote:
    > >>> On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    > >>>> 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?
     
    , Apr 10, 2009
    #7
  8. wrote:
    > On Apr 10, 7:58 pm, Victor Bazarov <> wrote:
    >> wrote:
    >>> On Apr 10, 7:32 pm, Victor Bazarov <> wrote:
    >>>> wrote:
    >>>>> On Apr 10, 7:10 pm, Victor Bazarov <> wrote:
    >>>>>> 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
     
    Victor Bazarov, Apr 10, 2009
    #8
  9. James Kanze Guest

    On Apr 10, 7:43 pm, Victor Bazarov <> wrote:
    > 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:
    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
     
    James Kanze, Apr 10, 2009
    #9
  10. 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.
     
    Juha Nieminen, Apr 11, 2009
    #10
  11. Guest

    On Apr 11, 8:44 pm, Juha Nieminen <> wrote:
    > 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.


    The input is from a binary file which contains series of octets.The
    output is another file if the MAC address matches.
     
    , Apr 12, 2009
    #11
  12. wrote:
    > 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.
     
    Juha Nieminen, Apr 12, 2009
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    759
    Ian Collins
    Dec 7, 2006
  2. namespace1
    Replies:
    3
    Views:
    928
  3. shailesh
    Replies:
    1
    Views:
    474
    Sion Arrowsmith
    Jan 28, 2008
  4. Alessio Pace
    Replies:
    0
    Views:
    650
    Alessio Pace
    Jan 7, 2009
  5. Aldo Ceccarelli
    Replies:
    7
    Views:
    379
    Aldo Ceccarelli
    Apr 19, 2011
Loading...

Share This Page