user1710944
user1710944

Reputation: 1459

Mac Address format from string

    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 ?

Upvotes: 6

Views: 8029

Answers (5)

Jeremy
Jeremy

Reputation: 863

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.

Upvotes: 3

Pierre
Pierre

Reputation: 9052

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 :

Upvotes: 0

randoms
randoms

Reputation: 2783

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;
    }

Upvotes: 1

Lorenzo Santoro
Lorenzo Santoro

Reputation: 512

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.

Upvotes: 0

Adil
Adil

Reputation: 148150

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

Upvotes: 5

Related Questions