Skip to content

Commit

Permalink
Add more allowed MAC-Address-Formates for Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Jan 18, 2024
1 parent 2464335 commit 62a5d7a
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions RDMSharp/RDM/MACAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,29 @@ public MACAddress(params byte[] bytes)
}
public MACAddress(in string macAddress) : this()
{
Regex regex = new Regex(@"^([A-Fa-f0-9]{1,2})\:([A-Fa-f0-9]{1,2})\:([A-Fa-f0-9]{1,2})\:([A-Fa-f0-9]{1,2})\:([A-Fa-f0-9]{1,2})\:([A-Fa-f0-9]{1,2})$");
var match = regex.Match(macAddress);
B1 = byte.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
B2 = byte.Parse(match.Groups[2].Value, System.Globalization.NumberStyles.HexNumber);
B3 = byte.Parse(match.Groups[3].Value, System.Globalization.NumberStyles.HexNumber);
B4 = byte.Parse(match.Groups[4].Value, System.Globalization.NumberStyles.HexNumber);
B5 = byte.Parse(match.Groups[5].Value, System.Globalization.NumberStyles.HexNumber);
B6 = byte.Parse(match.Groups[6].Value, System.Globalization.NumberStyles.HexNumber);
Regex regex6g = new Regex(@"^([A-Fa-f0-9]{1,2})[\:\-\s]([A-Fa-f0-9]{1,2})[\:\-\s]([A-Fa-f0-9]{1,2})[\:\-\s]([A-Fa-f0-9]{1,2})[\:\-\s]([A-Fa-f0-9]{1,2})[\:\-\s]([A-Fa-f0-9]{1,2})$");
var match = regex6g.Match(macAddress);
if (!match.Success)
{
Regex regex3g = new Regex(@"^([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})\.([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})\.([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$");
match = regex3g.Match(macAddress);
}
if (!match.Success)
{
Regex regex0g = new Regex(@"^([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$");
match = regex0g.Match(macAddress);
}
if (match.Success)
{
B1 = byte.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
B2 = byte.Parse(match.Groups[2].Value, System.Globalization.NumberStyles.HexNumber);
B3 = byte.Parse(match.Groups[3].Value, System.Globalization.NumberStyles.HexNumber);
B4 = byte.Parse(match.Groups[4].Value, System.Globalization.NumberStyles.HexNumber);
B5 = byte.Parse(match.Groups[5].Value, System.Globalization.NumberStyles.HexNumber);
B6 = byte.Parse(match.Groups[6].Value, System.Globalization.NumberStyles.HexNumber);
}
else
throw new Exception($"The given string\"{macAddress}\" is not matchable to any known MAC-Address format");
}

public MACAddress(IEnumerable<byte> enumerable)
Expand Down

0 comments on commit 62a5d7a

Please sign in to comment.