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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
2pietjuh2
  • 879
  • 2
  • 13
  • 29
  • You should probably consider a regular expression like this http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address – Marc Nov 27 '12 at 12:47
  • isn't ragex only for finding something in a style, i'm trying to convert the style :) – 2pietjuh2 Nov 27 '12 at 12:49
  • 1
    Not an exact duplicate, but pretty much the same problem: http://stackoverflow.com/questions/188510/how-to-format-a-string-as-a-telephone-number-in-c-sharp – Kevin Gosse Nov 27 '12 at 12:52
  • 1
    No you can replace things with regex as well. – Marc Nov 27 '12 at 12:54

4 Answers4

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));
Marc
  • 6,749
  • 9
  • 47
  • 78
  • Good solution but you shoud encapsulate it in a Custom Format Providers for DRY. – jlvaquero Nov 27 '12 at 13:21
  • 2
    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 Nov 27 '12 at 13:38
  • @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! – Marc Nov 27 '12 at 14:29
  • 1
    @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](http://msdn.microsoft.com/en-us/library/bb762932.aspx). – Marc Nov 27 '12 at 14:40
6
string input = "0018103AB839";

var output = string.Join(":", Enumerable.Range(0, 6)
    .Select(i => input.Substring(i * 2, 2)));
Jon B
  • 51,025
  • 31
  • 133
  • 161
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);
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
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

Skint007
  • 195
  • 5
  • 15