This article describes the Java programming method to traverse all MACs between two MAC addresses. Share it for your reference, as follows:
When managing the issued device in the background, the device MAC field is often used, which can identify a unique device. However, when storing MAC addresses in batches in the database, if parsed text is added line by line, it will inevitably appear complicated to operate, and MAC address text needs to be generated in advance. In fact, the MAC addresses are incremented one by one according to hexadecimal, so it is possible to enumerate all MAC addresses by just giving one interval. The following is a function encapsulated by the author to enumerate all MACs in the interval through two MAC addresses.
/** Output all MAC addresses in the two MAC intervals*/ public static void countBetweenMac(String macStart, String macEnd){ long start = turnMacToLong(macStart); long end = turnMac ToLong(macEnd); String prefix = macStart.substring( 0,9); String hex = null; String suffix = null; StringBuffer sb = null; for(long i=start; i< end +1; i++){ hex = Long.toHexString(i); suffix = hex.substring (hex.length()-6); sb = new StringBuffer(suffix); sb.insert(2, ":"); sb.insert(5, ":"); System.out.println(prefix + sb. toString()); } }/** Convert MAC to a number*/ public static long turnMacToLong(String MAC){ String hex = MAC.replaceAll("//:", ""); long longMac = Long.parseLon g( hex, 16); return longMac; }Also, calculate the number function in the MAC between two MACs:
/** Calculate the total number of MACs in the interval*/ public static long countMac1(String macStart, String macEnd){ String hexStart = macStart.replaceAll("//:", ""); String hexEnd = macEnd .replaceAll("//: ", ""); long start = Long.parseLong(hexStart, 16); long end = Long.parseLong(hexEnd, 16); return end-start+1; }/** Calculate the total number of MACs in the interval*/ public static long countMac(String macStart, String macEnd){ String[] start = macStart.split("//:"); String[] end = macEnd.split("//:"); int x,y,z; int a,b,c; x = Integer.parseInt(start[3],16); y = Integer.parseInt(start[4],16); z = Integer.parseInt(start[5],16); a = Integer.parseInt(end[3],16); b = Integer.parseInt(end[4],16); c = Integer.parseInt(end[5],16); return (ax)*16*16*16 + (by)*16*16 + c-z+1; }I hope this article will be helpful to everyone's Java programming.