JAVA MAC 比较大小
2个MAC
String mac_1="AAAAAAAAAAAA";
String mac_1="AAAAAAAAAAAB";
方法一:
int a = 0;
a = mac_1.compareTo(mac_2);
如果a>0 则 mac_1的16进制> mac_2的16进制 反之相反。
方法二:
字符转换成Long类型进行比较
Long mac_1_16 = Long.parseLong(mac_1, 16);
Long mac_2_16 = Long.parseLong(mac_1, 16);
mac_1_16和mac_2_16 直接比较即可。
下面是一个网络设备mac地址判断是不是在某一个范围之类的例子:
只展示接口功能,希望有些人可以用的着,就不详细说明了
public boolean checkEnbMacInRange(String macRangeStr, String enbMac) throws IOException { boolean inRange = false; List<String> licenseRangelist = getMacOuiSectionList(macRangeStr.toUpperCase()); enbMac.replace(":", "").toUpperCase(); String macOuiRange = licenseRangelist.get(0); String macOuiEnb = enbMac.substring(0, 6); logger.info("macOuiRange : " + macOuiRange); logger.info("macOuiEnb : " + macOuiEnb); Long enbMacSequenceLong = null; if (macOuiRange.equals(macOuiEnb)) { String enbMacSequenceStr = enbMac.substring(6, enbMac.length()); enbMacSequenceLong = Long.parseLong(enbMacSequenceStr, 16); for (int i = 1; i < licenseRangelist.size(); i++) { String rangeStart = licenseRangelist.get(i).substring(0, 6); String rangeEnd = licenseRangelist.get(i).substring(6, 12); //方法 二 Long rangeStartLong16 = Long.parseLong(rangeStart, 16); Long rangeSEndLong16 = Long.parseLong(rangeEnd, 16); logger.info("rangeStart : " + rangeStartLong16); logger.info("rangeSEnd : " + rangeSEndLong16); logger.info("enbMac : " + enbMacSequenceLong); if (rangeStartLong16 <= enbMacSequenceLong && enbMacSequenceLong <= rangeSEndLong16) { logger.info("true====" ); inRange = true; break; } //方法 一 int a = 0; int b = 0; a = enbMacSequenceStr.compareTo(rangeStart); b = enbMacSequenceStr.compareTo(rangeEnd); logger.info("a = " + a ); logger.info("b = " + b ); if (a >= 0 && b <= 0) { logger.info("true====" ); inRange = true; break; } } } return inRange; } /** * 拼装mac区间信息提供文件信息展示 * * @param macRangeStr * @return * @throws IOException */ public String getMacSectionStr(String macRangeStr) throws IOException { List<String> licenseRangelist = getMacOuiSectionList(macRangeStr); StringBuilder macSection = new StringBuilder(); String macOuiRange = licenseRangelist.get(0).toUpperCase(); for (int i = 1; i < licenseRangelist.size(); i++) { String rangeStart = licenseRangelist.get(i).substring(0, 6); String rangeEnd = licenseRangelist.get(i).substring(6, 12); macSection.append(macOuiRange).append(rangeStart); macSection.append("-"); macSection.append(macOuiRange).append(rangeEnd); macSection.append(","); } macSection.deleteCharAt(macSection.length() - 1); return macSection.toString(); } /** * 获取mac规则中的OUI,和mac区间范围信息 * * @param macRangeStr * @return * @throws IOException */ public List<String> getMacOuiSectionList(String macRangeStr) throws IOException { List<String> licenseRangelist = new ArrayList<String>(); if (CommMethod.isNotEmpty(macRangeStr)) { String MAC_OUI = macRangeStr.substring(0, 6); String MACListStr = macRangeStr.substring(9, macRangeStr.length()); macRangeStr = MAC_OUI + "," + MACListStr; String[] macArray = macRangeStr.split(","); licenseRangelist = Arrays.asList(macArray); } return licenseRangelist; }