locked
How to get MAC address from a string. RRS feed

  • Question

  • Im stuck with a problem of extracting a mac address from a string.

    Basically my program listens for tcp/ip requests and it receives a string. Not always the same length. But always contains a mac address.

    How can I take this string and get the mac address out of it and put it into a variable to use later?

     

    Thanks.

    Wednesday, February 16, 2011 2:06 AM

Answers

  • Hi,

    there are several Formats for a MAC-Address. I think the most common forms are xx:xx:xx:xx:xx:xx and xx-xx-xx-xx-xx-xx. But you can also write it as xxxxxxxxxxxx or xx.xx.xx.xx.xx.xx and even xxxx.xxxx.xxxx. You could even write them as unencoded bytes (binary).

    To match all of them (except the binary representation), you can use

    [0-9a-fA-F]{2}(?:[-.:]?[0-9a-fA-F]{2}){5}

    If you only want one specific format, adjust [-.:]? to your needs (e.g. remove ? if there is allways a seperator between all of the values. Remove . and : if this seperator is always a hyphen).

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by Jesse HouwingMVP Wednesday, February 16, 2011 12:26 PM
    • Edited by WolfgangKluge Thursday, February 17, 2011 3:21 PM change order of characters in set from .-: to -.: to prevent - to specify a range
    • Marked as answer by Martin Dixon Saturday, February 19, 2011 7:35 AM
    Wednesday, February 16, 2011 8:41 AM

All replies

  • Hi,

    there are several Formats for a MAC-Address. I think the most common forms are xx:xx:xx:xx:xx:xx and xx-xx-xx-xx-xx-xx. But you can also write it as xxxxxxxxxxxx or xx.xx.xx.xx.xx.xx and even xxxx.xxxx.xxxx. You could even write them as unencoded bytes (binary).

    To match all of them (except the binary representation), you can use

    [0-9a-fA-F]{2}(?:[-.:]?[0-9a-fA-F]{2}){5}

    If you only want one specific format, adjust [-.:]? to your needs (e.g. remove ? if there is allways a seperator between all of the values. Remove . and : if this seperator is always a hyphen).

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by Jesse HouwingMVP Wednesday, February 16, 2011 12:26 PM
    • Edited by WolfgangKluge Thursday, February 17, 2011 3:21 PM change order of characters in set from .-: to -.: to prevent - to specify a range
    • Marked as answer by Martin Dixon Saturday, February 19, 2011 7:35 AM
    Wednesday, February 16, 2011 8:41 AM
  • Spot on thank you.

    I'm also having a problem with the WebBrowser control.

    When I give it a page to navigate to, nothing happens, just stays on starting page.

     

    I am doing:

    webBrowser1.Navigate( "url here" );

    Saturday, February 19, 2011 7:40 AM