可以使用以下方法将 String 类型的 MAC 地址转换为 int 数组,数组长度为 6:

public static int[] convertMacAddress(String macAddress) {
    String[] macParts = macAddress.split(":");
    int[] result = new int[6];
    for (int i = 0; i < 6; i++) {
        result[i] = Integer.parseInt(macParts[i], 16);
    }
    return result;
}

使用示例:

String macAddress = 'AB:CD:EF:12:34:56';
int[] macArray = convertMacAddress(macAddress);
for (int i = 0; i < macArray.length; i++) {
    System.out.println(macArray[i]);
}

输出结果:

171
205
239
18
52
86

注意:上述代码假设输入的 MAC 地址是合法的,即格式为 XX:XX:XX:XX:XX:XX,其中 X 为十六进制数。如果输入的 MAC 地址格式不正确,将会抛出 NumberFormatException 异常。

Java 将字符串 MAC 地址转换为长度为 6 的 int 数组

原文地址: https://www.cveoy.top/t/topic/qhpH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录