1

is there a way how to convert string from textbox in this format (00:15:5D:03:8D:01) to MacAddress variable? I'm using PcapDotNet library.

Torrado36
  • 167
  • 1
  • 2
  • 10

2 Answers2

2

You can use the string constructor:

var macAddress = new MacAddress(textBox1.Text);

As you can see from the code linked, it will throw an exception if you don't pass in a valid mac address. You could perform a check before you pass it in:

string mac = textBox1.Text;
var acceptableChars = ":0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";        
if (mac.All(c => acceptableChars.IndexOf(c) != -1) && mac.Count(c => c == ':') == 5 && mac.Length == 17)
{
    var macAddress = new MacAddress(mac);
}
else
{
    // invalid mac
}

This ensures that all characters are :, 0-9, a-f (and A-F), and that there are 6 sections and the total length is 17. This will help you avoid getting an exception if an invalid mac is entered.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
0

Just use the constructor of MacAddress, it accepts a string:

MacAddress address = MacAddress(textbox.Text);

See also: https://github.com/PcapDotNet/Pcap.Net/blob/master/PcapDotNet/src/PcapDotNet.Packets/Ethernet/MacAddress.cs

adjan
  • 13,371
  • 2
  • 31
  • 48