0

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

Andy
  • 383
  • 1
  • 6
  • 23
  • 2
    You probably want to parse it into a `long` variable, increment that, then render that again as a MAC address format. – Alejandro Nov 03 '20 at 19:13
  • 2
    That said, why do you even want to do this? – Alejandro Nov 03 '20 at 19:13
  • Because my boss said so. :) We will be managing a database of MAC addresses that we've purchased. – Andy Nov 03 '20 at 19:19
  • 1
    To what end? Why would you want the MAC address of a device you *don't* own in your database? – madreflection Nov 03 '20 at 19:23
  • 1
    The second (right) half of the MAC address is already incremented by the manufacturer for each device that rolls off the assembly line. – madreflection Nov 03 '20 at 19:24
  • @Alejandro, if I parse it into a long variable does that convert the hex values? Sorry, I'm very new to this. – Andy Nov 03 '20 at 19:24
  • @Andy The hex exists only on the string representation. By turning into a long, you get a real number to do math with, then you can turn that number back into a proper string for other uses, getting the hex again, after the increment. – Alejandro Nov 03 '20 at 19:28
  • @Alejandro, I updated my question with a start to some code. Would you mind commenting? That may get me to a long but I don't know how to convert it back to a string in HEX format. – Andy Nov 03 '20 at 19:53
  • See third duplicate for how to format the new value as hex again. You can use `':'` instead of `' '` for the `string.Join()`. Alternatively, you could just format the integer value back as hex, and then insert the colons using `StringBuilder`. – Peter Duniho Nov 03 '20 at 19:58
  • @PeterDuniho, the 3rd link does not help. Can you please look at the updated code in my question and offer some advise? It shows what I get when I use that code. Also I had to change the int to a long because the number was to big for an int in the parse. – Andy Nov 09 '20 at 18:44
  • Sometime you need to read the comments to make full use of an answer. Though, the other answers to that question also work. So you could look at those instead. That said, I've edited the answer to merge the needed changes pointed out in the comments, into the answer itself. – Peter Duniho Nov 09 '20 at 19:16
  • @PeterDuniho, nevermind. I finally found the answer by diging in the haystack for a week.. No idea why my question was closed. This worked: sMACAddressOut = string.Join(":", BitConverter.GetBytes(iLastMACAddressHex).Reverse() .Select(b => b.ToString("X2"))).Substring(6); – Andy Nov 09 '20 at 19:18
  • _"No idea why my question was closed"_ -- it was closed because in spite of being too open-ended (which in and of itself is a reason for closing), the answers to each individual piece of your question are already here on Stack Overflow, so there's no need for any new answers to be posted here. – Peter Duniho Nov 09 '20 at 19:20

0 Answers0