I have a seemingly simple request that I have no idea how to do in C#. I have searched and only found answers using other languages.
I have a string that includes a MAC address. For example, string sMyMACAddress = "00:1D:FE:12:37:1A";
How do I take this MAC address, increment it by 1, and pop out the resultant new next-in-sequence MAC Address in a new string such as sMyNextMACAddress
?
Can someone help me with this or at least point me in the right direction?
I've tried the following after the latest suggestions. UPDATE: I've provided the answer below as my question was closed prior to an answer being accepted.
string sMACAddressIn = "00:1D:FE:12:37:1A";
string sMACAddressOut = "";
Console.WriteLine("MAC Address going in: " + sMACAddressIn);
sMACAddressIn = sMACAddressIn.Replace(":", ""); // Format this otherwise you get an incorrect format error on numberstyles.hexnumber
long iLastMACAddressHex = long.Parse(sMACAddressIn, NumberStyles.HexNumber);
iLastMACAddressHex++;
sMACAddressOut = string.Join(":", BitConverter.GetBytes(iLastMACAddressHex).Reverse() .Select(b => b.ToString("X2"))).Substring(6);
Console.WriteLine("MAC Address going out: " + sMACAddressOut);
Console.Read();
I get the following result.
MAC Address going in: 00:1D:FE:12:37:1A
MAC Address going out: 00:1D:FE:12:37:1B