Generate a random mac address

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

I am looking for a method in C# which generates a random MAC number. Google is pretty thin on that one.

Thx a lot

SOLUTION:

With the help of Yahia I was able to code the following solution. Thx again!

回答1:

There is no such method in the .NET framework...

You will need to write one yourself - read the format description, use a random generator to get 6 random numbers between 0 and 255, setup the 2 relevant bits (for globally unique/locally administered) as need be and then transform the number to hex (i.e. X2, 2 digits per number, left padded with 0) and join these together with : as delimiter...



回答2:

A slightly less verbose solution (which I think still achieves the same outcome):

public static string GetRandomMacAddress() {     var random = new Random();     var buffer = new byte[6];     random.NextBytes(buffer);     var result = String.Concat(buffer.Select(x => string.Format("{0}:", x.ToString("X2"))).ToArray());     return result.TrimEnd(':'); } 

This gives a formatted MAC, remove string.Format and Trim if unformatted is required



回答3:

MUG4N's solution has a problem. You have to tweak the LEAST significant two bits not the MOST significant two.

So instead of

b = setBit(b, 6); //--> set locally administered b = unsetBit(b, 7); // --> set unicast  

it should be

b = setBit(b, 1); //--> set locally administered b = unsetBit(b, 0); // --> set unicast  

Also the unsetBit() is incorrect. The relevant line should be

return unchecked((byte)(b & (byte)~(0x01 << BitNumber))); 

Of course it would probably be simpler to change it to this:

if (i == 0) {     b = (byte)((b & 0xFE) | 0x02) //-->set locally administered and unicast } 


回答4:

Small update for those of you who have a problem generating a new MAC address for your Wifi-adapter. You just have to set the first octet of MAC address to "02", instead on what normally is "00". Setting first octet "02" actually sets the b2 bit indicating that the MAC address is locally administered.

You can read more about it here: http://blog.technitium.com/2011/05/tmac-issue-with-wireless-network.html

Code:

public static string GetRandomWifiMacAddress() {     var random = new Random();     var buffer = new byte[6];     random.NextBytes(buffer);     buffer[0] = 02;     var result = string.Concat(buffer.Select(x => string.Format("{0}", x.ToString("X2"))).ToArray());     return result; } 


回答5:

Here is a helper class to generate random mac.

public static class MacAddress {     private static readonly Random Random = new Random();      public static string GetSignatureRandomMac(string generic = "AA")     {         string[] macBytes = new[]         {             generic,             generic,             generic,             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X")         };          return string.Join("-", macBytes);     }      public static string GetRandomMac()     {         string[] macBytes = new[]         {             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X"),             Random.Next(1, 256).ToString("X")         };          return string.Join("-", macBytes);     } } 

Usage:

Console.WriteLine(MacAddress.GetRandomMac()); Console.WriteLine(MacAddress.GetSignatureRandomMac()); Console.WriteLine(MacAddress.GetSignatureRandomMac("BB")); 


回答6:

There isn't a function within .NET to generate MAC addresses. It would have to be written.

MAC Addresses are generally meant to be unique and set by the OEM of the NIC. Different manufacturing have a certain allocated prefix. A sample list can be found here; https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf

Off the top of my head, I don't know of any libraries that exist to generate MAC addresses (as it's not a common requirement), however it wouldn't be too difficult to create one as an address is simply 6 hexadecimal values from 00 to FF separated by a colon.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!