14

I have a mac address which is formatted like 0018103AB839 and I want to display it like: 00:18:10:3A:B8:39

I am trying to do this with string.Format but I cant really find the exact syntax.

right now I am trying something like this:

    string macaddress = 0018103AB839;
    string newformat = string.Format("{0:00:00:00:00:00:00}", macaddress);

Is this even possible? or should I use string.Insert?

4
  • You should probably consider a regular expression like this stackoverflow.com/questions/4260467/… Commented Nov 27, 2012 at 12:47
  • isn't ragex only for finding something in a style, i'm trying to convert the style :) Commented Nov 27, 2012 at 12:49
  • 1
    Not an exact duplicate, but pretty much the same problem: stackoverflow.com/questions/188510/… Commented Nov 27, 2012 at 12:52
  • 1
    No you can replace things with regex as well. Commented Nov 27, 2012 at 12:54

4 Answers 4

13

Reformat a string to display it as a MAC address:

var macadres = "0018103AB839";

var regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
var replace = "$1:$2:$3:$4:$5:$6";
var newformat = Regex.Replace(macadres, regex, replace);    

// newformat = "00:18:10:3A:B8:39"

If you want to validate the input string use this regex (thanks to J0HN):

var regex = String.Concat(Enumerable.Repeat("([a-fA-F0-9]{2})", 6));
Sign up to request clarification or add additional context in comments.

4 Comments

Good solution but you shoud encapsulate it in a Custom Format Providers for DRY.
Well, there are two possible flaws with regexes: (1) it does not work on valid input, (2) it work for invalid input. So, your solution are subject for the second flaw: try fed it with "!@#$%^&*()-+" - it will happily display it as a mac address. I understand that OP said nothing about validation, but it would be nice if you replace dots with [a-fA-F0-9], or at least \w.
@J0HN Yes I agree but as you said the OP asked just about displaying a string as a MAC address. So the question here would be: is it really better to display nothing if the input is not a valid MAC address? I think it depends on the situation but I will add your validation as an option to my answer, thank you!
@jlvaquero Good point, thanks, but I think thats a bit oversized for this situation. But whoever wants to clean this up, feel free to read about Custom Format Providers.
6
string input = "0018103AB839";

var output = string.Join(":", Enumerable.Range(0, 6)
    .Select(i => input.Substring(i * 2, 2)));

Comments

5

Suppose that we have the Mac Address stored in a long. This is how to have it in a formatted string:

ulong lMacAddr = 0x0018103AB839L;
string strMacAddr = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
    (lMacAddr >> (8 * 5)) & 0xff, 
    (lMacAddr >> (8 * 4)) & 0xff,
    (lMacAddr >> (8 * 3)) & 0xff,
    (lMacAddr >> (8 * 2)) & 0xff,
    (lMacAddr >> (8 * 1)) & 0xff,
    (lMacAddr >> (8 * 0)) & 0xff);

Comments

0

Using J0HN's suggested regex, I made a tweak to it and put it in an extension method.

public static string FormatAsMacAddress(this string mac)
{
    var regex = "^([a-fA-F0-9]{2}){6}$";
    return string.Join(":", Regex.Match(mac, regex).Groups[1].Captures.Select(x => x.Value));
}

You can use it like this:

var macAddress = "0018103AB839";
macAddress.FormatAsMacAddress();

Outputs:

00:18:10:3A:B8:39

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.