Click here to Skip to main content
15,890,282 members
Home / Discussions / C#
   

C#

 
QuestionString format for MAC-address Pin
Mc_Topaz24-Mar-11 4:22
Mc_Topaz24-Mar-11 4:22 
AnswerRe: String format for MAC-address Pin
Luc Pattyn24-Mar-11 4:37
sitebuilderLuc Pattyn24-Mar-11 4:37 
AnswerRe: String format for MAC-address Pin
J4amieC24-Mar-11 5:38
J4amieC24-Mar-11 5:38 
AnswerRe: String format for MAC-address Pin
Richard MacCutchan24-Mar-11 5:53
mveRichard MacCutchan24-Mar-11 5:53 
AnswerRe: String format for MAC-address PinPopular
musefan24-Mar-11 6:56
musefan24-Mar-11 6:56 
AnswerRe: String format for MAC-address Pin
Ennis Ray Lynch, Jr.24-Mar-11 7:54
Ennis Ray Lynch, Jr.24-Mar-11 7:54 
AnswerRe: String format for MAC-address PinPopular
#realJSOP24-Mar-11 9:31
mve#realJSOP24-Mar-11 9:31 
AnswerRe: String format for MAC-address PinPopular
#realJSOP24-Mar-11 9:58
mve#realJSOP24-Mar-11 9:58 
OH WAIT! How about a MACAddress class? (The following is untested code)

C#
public class MACAddress
{
    const string separator = "-";
    public int[] Octets = new int[6];
    public string Mac 
    {
        get 
        { 
            StringBuilder str = new StringBuilder();
            for(int i = 0; i < Octets.Length; i++)
            {
                str.AppendFormat("{0:x2}", Octets[i]);
                if (i < Octets.Length - 1)
                {
                    str.Append(separator);
                }
            }
    }

    public MACAddress(string address)
    {
        if (ValidateAddress(ref address))
        {
            Parse(address)
        }
    }

    private bool Validate(ref string address)
    {
        address = address.Replace("-", "").Replace(":", "").Replace(" ", "").ToUpper().Trim();
        if (address.Length != 12)
        {
            throw new Exception("Invalid mac address specified. Must be exactly 12 valid alphanumeric characters");
        }
        string validCharacters = "0123456789ABCDEF";
        var count = (from c in address 
                     where !validCharacters.Contains(c)
                     select c).Count;
        if (count > 0)
        {
            throw new Exception("Invalid MAC address specified. One or more invalid characters detected.");
        }
    }

    private void Parse(string address)
    {
        for (int i = 10; i > 0; i-2)
        {
            address = address.Insert(i,":");
        }
        string[] parts = address.Split(":");
        for (int i = 0; i < parts.Length; i++)
        {
            Int32.TryParse(parts[i], out Octets[i]);
        }
    }
}

".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997


GeneralRe: String format for MAC-address Pin
HimanshuJoshi24-Mar-11 10:04
HimanshuJoshi24-Mar-11 10:04 
GeneralRe: String format for MAC-address Pin
fjdiewornncalwe25-Mar-11 2:55
professionalfjdiewornncalwe25-Mar-11 2:55 
QuestionControl forms of an app Pin
dj_jeff24-Mar-11 3:43
dj_jeff24-Mar-11 3:43 
AnswerRe: Control forms of an app Pin
BobJanova24-Mar-11 5:23
BobJanova24-Mar-11 5:23 
AnswerRe: Control forms of an app Pin
Nitheesh George24-Mar-11 20:30
Nitheesh George24-Mar-11 20:30 
GeneralRe: Control forms of an app Pin
dj_jeff24-Mar-11 21:28
dj_jeff24-Mar-11 21:28 
GeneralRe: Control forms of an app Pin
Nitheesh George24-Mar-11 22:45
Nitheesh George24-Mar-11 22:45 
GeneralRe: Control forms of an app Pin
dj_jeff25-Mar-11 0:11
dj_jeff25-Mar-11 0:11 
QuestionDatagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Davey_guan24-Mar-11 3:34
Davey_guan24-Mar-11 3:34 
AnswerRe: Datagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Henry Minute24-Mar-11 17:12
Henry Minute24-Mar-11 17:12 
GeneralRe: Datagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Davey_guan25-Mar-11 19:48
Davey_guan25-Mar-11 19:48 
GeneralRe: Datagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Henry Minute26-Mar-11 5:03
Henry Minute26-Mar-11 5:03 
GeneralRe: Datagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Davey_guan26-Mar-11 20:01
Davey_guan26-Mar-11 20:01 
GeneralRe: Datagridviewcomboboxcolumn SelectedIndexChanged not work properly Pin
Henry Minute26-Mar-11 5:40
Henry Minute26-Mar-11 5:40 
Questionneed help in c# coding Pin
ULKA RAMAKRISHNAN24-Mar-11 2:16
ULKA RAMAKRISHNAN24-Mar-11 2:16 
AnswerRe: need help in c# coding Pin
Ravi Bhavnani24-Mar-11 2:19
professionalRavi Bhavnani24-Mar-11 2:19 
AnswerRe: need help in c# coding Pin
RobCroll24-Mar-11 2:26
RobCroll24-Mar-11 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.