6
    NetworkInterface[] arr = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface item in arr)
    {
        PhysicalAddress mac = item.GetPhysicalAddress();
    }

It returns the value of 00E0EE00EE00 whereas I want it to display something like 00:E0:EE:00:EE:00 but i need to use .Net 4

any ideas ?

5 Answers 5

5

You can use String.Insert method of string class to add :

string macAddStr = "00E0EE00EE00";
string macAddStrNew = macAddStr;
int insertedCount = 0;
for(int i = 2; i < macAddStr.Length; i=i+2)  
   macAddStrNew = macAddStrNew.Insert(i+insertedCount++, ":");

//macAddStrNew will have address 00:E0:EE:00:EE:00
Sign up to request clarification or add additional context in comments.

1 Comment

that's put ":" only one time every iteration in different place
3

I know this was answered a while ago, but I just wanted to clarify that the preferred solution is usually to create a reusable extension method for the PhysicalAddress class. Since it is a simple data class, and is likely not to change, this is better for reusability reasons. I will use Lorenzo's example because I like it the most, but you can use whichever routine suits you.

public static class PhysicalAddressExtensions
{
    public static string ToString(this PhysicalAddress address, string separator)
    {
        return string.Join(separator, address.GetAddressBytes()
                                             .Select(x => x.ToString("X2")))
    }
}

Now you can just use the extension method from now on like this:

NetworkInterface[] arr = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface item in arr)
{
    PhysicalAddress mac = item.GetPhysicalAddress();
    string stringFormatMac = mac.ToString(":");
}

Remember that the PhysicalAddress.Parse only accepts the RAW hex or dash separated values, in case you wanted to parse it back into an object. So stripping the separator character before you parse is important.

Comments

1

You can do something like this:

    string macAddr = "AAEEBBCCDDFF";
    var splitMac = SplitStringInChunks(macAddr);

    static string SplitStringInChunks(string str)
    {
        for (int i = 2; i < str.Length; i += 3)
             str =  str.Insert(i, ":");
        return str;
    }

Comments

0

If you want, you can use string.Join and Linq .Select():

NetworkInterface[] arr = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface item in arr)
{
    PhysicalAddress mac = item.GetPhysicalAddress();
    string stringFormatMac = string.Join(":", mac.GetAddressBytes().Select(varByte => varByte.ToString("X2")));
}

Hope it helps.

Comments

0

Method that validate and formats mac to XX:XX:XX:XX:XX:XX format.

Returns null if its not valid.

public string FormatMACAddress(string MacAddress)
{
    if (string.IsNullOrEmpty(MacAddress)) return null;

    MacAddress = MacAddress.ToUpper()
                           .Replace(" ","")
                           .Replace("-", ":") //'AA-BB-CC-11-22-33', 'AA:BB:CC:11:22:33' [17 Chars]
                           .Trim();

    if (!MacAddress.Contains(':') && MacAddress.Length == 12) //'AABBCC112233' [12 Chars]
    {
        for (int i = 2; i < MacAddress.Length; i += 3) MacAddress = MacAddress.Insert(i, ":");
    }

    if (MacAddress.Length != 17) return null; //'AA:BB:CC:11:22:33' [17 Chars]
    if (MacAddress.Count(c => c == ':') != 5) return null;

    //Hex check here, '0-9A-F' and ':'

    return MacAddress;
}

I have not tested this yet, but should work. Another check to add is to check whether all characters are hex values 0-9A-F and :

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.