Created
January 14, 2024 16:18
-
-
Save spiritedRunning/02e9027298fd5fc1da9d8a8542ff0d6c to your computer and use it in GitHub Desktop.
mac address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generate random MAC address | |
public static String randomMACAddress() { | |
Random rand = new Random(); | |
byte[] macAddr = new byte[6]; | |
rand.nextBytes(macAddr); | |
macAddr[0] = (byte) (macAddr[0] & (byte) 254); | |
StringBuilder sb = new StringBuilder(18); | |
for (byte b : macAddr) { | |
if (sb.length() > 0) | |
sb.append(":"); | |
sb.append(String.format("%02x", b)); | |
} | |
return sb.toString(); | |
} | |
// check MAC | |
public static boolean checkMacAvailable(String macAddress) { | |
String patternMac = "^[a-f0-9]{2}(:[a-f0-9]{2}){5}$"; | |
return Pattern.compile(patternMac).matcher(macAddress).find(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment